C#C
C#3y 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();


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" };


The expected result is cust 1 and 3 but its not what I get. the attached image shows what I get.
image.png
Was this page helpful?