C
C#2y ago
jalwn

❔ Help with linq query

I have a list of cutomers that gets grouped according to their addresss and data property. I would like to select the first group where the data is equal to Klepto or some other thing. From the code I have I am expecting to retrieve customer 1 and customer 3 as they both have Klepto as their data.
c#
var getOneGroup = CustList.GroupBy(x => new
{
x.Address,
x.SomeData
})
.Select(x => x.FirstOrDefault(y => y.SomeData.Equals("Klepto"))).ToList();
c#
var getOneGroup = CustList.GroupBy(x => new
{
x.Address,
x.SomeData
})
.Select(x => x.FirstOrDefault(y => y.SomeData.Equals("Klepto"))).ToList();
Here are my customers, I add these to CustList later on.
c#
Customer cust1 = new Customer()
{ Name = "Cust1", Number = "7784875", Address = "Address1", SomeData = "Klepto" };
Customer cust2 = new Customer()
{ Name = "Cust2", Number = "7574477", Address = "Address2", SomeData = "Great Kisser" };
Customer cust3 = new Customer()
{ Name = "Cust3", Number = "7680611", Address = "Address1", SomeData = "Klepto" };
Customer cust4 = new Customer()
{ Name = "Cust4", Number = "76800802", Address = "Address4", SomeData = "Good" };
Customer cust5 = new Customer()
{ Name = "Cust5", Number = "7777777", Address = "Address5", SomeData = "Funny" };
c#
Customer cust1 = new Customer()
{ Name = "Cust1", Number = "7784875", Address = "Address1", SomeData = "Klepto" };
Customer cust2 = new Customer()
{ Name = "Cust2", Number = "7574477", Address = "Address2", SomeData = "Great Kisser" };
Customer cust3 = new Customer()
{ Name = "Cust3", Number = "7680611", Address = "Address1", SomeData = "Klepto" };
Customer cust4 = new Customer()
{ Name = "Cust4", Number = "76800802", Address = "Address4", SomeData = "Good" };
Customer cust5 = new Customer()
{ Name = "Cust5", Number = "7777777", Address = "Address5", SomeData = "Funny" };
The expected result is cust 1 and 3 but its not what I get. the attached image shows what I get.
3 Replies
LPeter1997
LPeter19972y ago
What's the question? Edit: ah it was too high post to see the pict This is your problem: .Select(x => x.FirstOrDefault(y => y.SomeData.Equals("Klepto"))).ToList(); You are projecting each group to their first element You need to filter instead:
var getOneGroup = CustList.GroupBy(x => new
{
x.Address,
x.SomeData
})
.Where(x => x.Any(y => y.SomeData.Equals("Klepto"))).First().ToList();
var getOneGroup = CustList.GroupBy(x => new
{
x.Address,
x.SomeData
})
.Where(x => x.Any(y => y.SomeData.Equals("Klepto"))).First().ToList();
Since LINQ has a ton of utility overloads, you can also add a predicate to First: .First(x => x.Any(y => y.SomeData.Equals("Klepto"))).ToList(); Essentially the same deal
jalwn
jalwnOP2y ago
Thank you very much. I was wondering why I keep getting nulls along with customer 1 but I never got customer 3. The query you provided works very well!
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server