简体   繁体   中英

Getting a List<Items> out of List<List<Items>>

I have a object called Items. I use that object like so.

List<List<Items>> myItems = new List<List<Items>>();

I want to know how to get a specific List<Items> out of the List<List<Items>> object. Currently I am using foreach loops with some rules to find a specific List<Items>

This is currently what I am doing

foreach (List<Items> db2Items in db2Data)
{
     foreach (List<Items> apiItems in apiData)
     {
          if (db2Items[0].Value == apiItems[0].Value && db2Items[27].Value == apiItems[27].Value)
          {
               /// Some other logic here
          }
     }
}

I was wanting to use LINQ to get the matching List<items> out of apiData and if I have a result then do the logic I wanted to do.

Not really sure what you're trying to accomplish, but if you want to get a list in a list based on a certain condition you can do it like this:

var item = db2Data.Where(x => x.Where(y => y.[value] == [YourCondition]).Any()).FirstOrDefault();

the "x.Where(y => y == [YourCondition]).Any()" will return true if the condition is met, and firstordefault will then return the list that meets the condition.

I have figured it out.

Using LINQ:

foreach (List<Items> db2Items in db2Data)
{
     IEnumerable<List<Items>> theItem = from item in apiData
                                        where item[0].Value == db2Items[0].Value && item[27]>Value == db2Items[27].Value
                                        select item;
     if (theItem.ToList().Count > 0)
     {
          // Do something
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM