C
C#14mo ago
popcorn

❔ query all items that match both properties.

I have a list of tuples as:
List<Tuple<int, string>> desiredValues = new List<Tuple<int, string>>() {};

desiredValues.Add(new Tuple(5, "abc"));
desiredValues.Add(new Tuple(8, "xyz"));
desiredValues.Add(new Tuple(10, "hjk"));


class MyCollectionItem {
public int Score {get; set;}
public string PlayerName {get; set;}
}
List<Tuple<int, string>> desiredValues = new List<Tuple<int, string>>() {};

desiredValues.Add(new Tuple(5, "abc"));
desiredValues.Add(new Tuple(8, "xyz"));
desiredValues.Add(new Tuple(10, "hjk"));


class MyCollectionItem {
public int Score {get; set;}
public string PlayerName {get; set;}
}
And I want to retrieve all items that match the int to score and string to PlayerName. How can I do that using LINQ? I don't want all the possible combinations of pair values.
13 Replies
Pobiega
Pobiega14mo ago
its unclear what you want. Do you have an instance of MyCollectionItem and want to see if it matches one of your tuples? or do you want to map your tuples into MyCollectionItems?
Angius
Angius14mo ago
Just use a .Where()
Pobiega
Pobiega14mo ago
or y'know, ask a question then immediately go offline 😄
Angius
Angius14mo ago
A, a cult classic in this server at this point lmao
popcorn
popcornOP14mo ago
I have a List of MyCoĺectionItem and I want to retrieve all of these that match a tuple from the other List
Angius
Angius14mo ago
Convert the list of tuples to list of MyCollectionItems with .Select(), then .Intersect() Alternatively, .Where() with a .Contains() inside But it'll prolly be less performant Ah, there's also .IntersectBy() Seems to be just what you need
popcorn
popcornOP14mo ago
Cannot convert them, for simplicity I removed other properties. The MyCollectionItem has a lot of other properties, but I need to match only on these two
Angius
Angius14mo ago
.IntersectBy() then
Pobiega
Pobiega14mo ago
go with IntersectBy then
popcorn
popcornOP14mo ago
Okay, thanks
Sunny99
Sunny9914mo ago
Just read about FROM, WHERE, SELECT, INTERSECTBY clause and how to use it to manipulate data, almost the same thing if you know about database
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?