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>
Any help would be appreciated.
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.
CodePudding user response:
It is not really clear how you identify which list within your list you require, but maybe this is the problem?
This is then why a List of lists in such a way is not really very practical. I would suggest to rather use a dictionary of lists.
Dictionary<string, List<Items>> myDict = new Dictionary<string, List<Items><();
The string then is your key (it can be any other type), and then you can just use the key to get the appropriate list
myDict.Add("list 1", list1);
myDict.Add("list 2", list2);
myDict.Add("list 3", list3);
List<Items> foundList = myDict["list 2"];
Or to be a bit safer, use trygetvalue
List<Items> foundList = null;
if (myDict.TryGetValue("list 2", out foundList )) {
// the key exists in the dictionary.
}
CodePudding user response:
Create a List with the desired items:
List<List<Items>> desiredItems = new List<List<Items>>();
Loop through every item inside your myItems:
foreach (var item in myItems)
{
if (specific rule)
{
desiredItems.Add(item)
}
}
I think it's this what you want.