I Love Stackoverflow
I Love Stackoverflow

Reputation: 6868

Unable to manupulate the list

I am having below class:

 public class statistics
    {
        public int Type { get; set; }
        public string Title { get; set; }
        public bool Flag { get; set; }
    }

Above is list of statistics class so it contains records like this:

1st record : Type = 1
             Title = "abc,ttt"
             flag= true

2nd Records : Type = 1
             Title = "xyz"
             flag= true

3rd Records : Type = 1
              Title = "lmn,ggg"
              flag= true

So here I would like to manipulate my statistics variable and so my statistics variable should contains 5 records like below:

1st record : Type = 1
             Title = "abc"
             flag= true


2nd record : Type = 1
             Title = "ttt"
             flag= true

3rd Records : Type = 
             Title = "xyz"
             flag= true

4th Records : Type = 1
              Title = "lmn"
              flag= true

5th Records : Type = 1
              Title = "ggg"
              flag= true

So as you can see that I would like to have a separate record if title contains comma-separated records.

For eg:1st record : Type = 1
             Title = "abc,ttt"
             flag= true

abc and ttt should split into two records as title contains comma-separated records.

This is how I am trying but unable to do it:

statistics = statistics.Select(o => o.Title.Split(',')).
                            Select(
                                        t => new statistics
                                        {
                                            Type = t.   // not working
                                        }
                                        ).ToList();

Upvotes: 0

Views: 89

Answers (4)

Slai
Slai

Reputation: 22876

A bit nicer with the query syntax, but don't use the same name statistics for both the class and list:

var result = (from s in statistics
              from a in s.Title.Split(',')
              select new statistics(){ Type = s.Type, Title = a, Flag = s.Flag }).ToList();

Upvotes: 1

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186748

It seems that you're looking for Split (to turn single comma separated Type into several items) and SelectMany (to flatten the collection):

List<statistics> source = .....;

var result = source
  .SelectMany(item => item.Title //split title 
     .Split(',')
     .Select(title => new statistics() {
        Type = item.Type,
        Title = title,
        Flag = item.Flag }))
  .ToList(); // finally, materialize into list

Upvotes: 3

Disappointed
Disappointed

Reputation: 1120

var result = statistics.SelectMany(s => s.Title.Split(new char[] { ',' }).
Select(t => new statistics() { Title = t, Flag = s.Flag, Type = s.Type }));

Upvotes: 1

Maksim Simkin
Maksim Simkin

Reputation: 9679

You need something like this:

var result = statistics.Select(s => s.Title.Split(',')
    .Select(x => new statistics {Type = s.Type, Flag = s.Flag, Title = x}))
    .SelectMany(s=>s)
    .ToList();

With this output:

enter image description here

Upvotes: 1

Related Questions