1

I am just wondering if there is a way to split a list item into two list items based on an object property using C# LINQ.

For example:

{
  "ItemName": "Test Item",
  "SubItemName": [
    {
      "Sub-Item-1",
      "Sub-Item-2",
    }
  ]
}

how to split this object by subItemName like:

{
  "ItemName": "Test Item Name",
  "SubItemName": "Sub-Item-1"
},
{
  "ItemName": "Test Item Name",
  "SubItemName": "Sub-Item-2",  
}
2
  • 2
    Can you show up the c# code you have so far so we can tell where you are getting stuck?
    – John Wu
    Mar 24, 2020 at 19:31
  • Always try to post a working code. You just showed us some JSON. How could we give a working code to you? (I tried to give one, below!) Mar 24, 2020 at 21:13

3 Answers 3

3

Yes, that is SelectMany. Let's look at a simple example:

class C
{
  public string Name { get; set; }
  public IEnumerable<string> Subs { get; set; }
}

Now let's make a bunch of them.

var cs = new List<C> {
  new C { Name = "Alice", Subs = new List<string> { "Orange", "Green" }},
  new C { Name = "Bobby", Subs = new List<string> { "Red", "Blue" }}
}

That is, we have

Alice
  Orange
  Green
Bobby
  Red
  Blue

You want a new list that is

Alice
  Orange
Alice
  Green
Bobby
  Red
Bobby
  Blue

Right?

Any time you want to "flatten" a list-of-lists you use SelectMany. Either in comprehension form:

var query = 
  from c in cs
  from s in c.Subs
  select new C { Name = c.Name, Subs = new List<string> { s } };  

Or in fluent form

var query = 
  cs.SelectMany(
    c => c.Subs,
    (c, s) => new C { Name = c.Name, Subs = new List<string> { s } });

which as you can see has the same structure, just a little harder to read.

If you then need a list out the other end:

var newList = query.ToList();

And you're done.

2
  • Hi Eric, Thank you so much for your help. Almost i finished my query and i solved most of them with your help. What if there are two Orange then it should be like; <br/> ``` Alice<br/> Orange<br/> Orange<br/> Count : 2<br/> Alice<br/> Green<br/> Count : 1<br/> Bobby<br/> Red<br/> Count : 1<br/> Bobby<br/> Blue<br/> Count : 1<br/> <br/> I mean that Eric
    – Cemal
    Mar 25, 2020 at 22:12
  • @Cemal: Then what you want to do is in the from s in c.Subs instead what you want to do is "(from g in c.Subs group by..." and turn the collection of subs into a collection of groups of the same thing. That gives you a sequence of groups where the element you've grouped on is Key, and the number of such elements is Count(), and you can then achieve your goal from there. Mar 25, 2020 at 23:06
1

A single item:

var item = new {
  ItemName = "Test Item",
  SubItemName = new string[]
    {
      "Sub-Item-1",
      "Sub-Item-2",
    }
};

I guess you have a collection of such items:

var items = new [] { item, /* other items */ };

And here is the LINQ:

var result =
    from i in items
    from si in i.SubItemName
    select new 
    {
        ItemName = i.ItemName,
        SubItemName = si
    };
0

use the LINQ .Select() Method on the collection

You can use the .Select() Method to project the things in your SubItems collection into a completely new form.

Since you didn't post the code of what you have so far, I will answer in general. Check out this code. :

public class WhateverYouHaveThere
{
    public string Name { get; set; }

    public List<string> SubItemName { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        //the thing you have
        var theThingYouHave = new WhateverYouHaveThere() { 
            Name = "Test Item",
            SubItemName = new List<string>() {
                "Sub-Item 1", 
                "SubItem 2" } 
        };

        //split the thing into a new list
        var theResultingListYouWant = theThingYouHave.SubItemName
            .Select(p => new WhateverYouHaveThere() { 
                Name = theThingYouHave.Name, 
                SubItemName = new List<string>() { p } }
            ).ToList();
    }
}

please accept as answer if that worked for you

greetings, Mike

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.