Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

I have a List<t> and I need to do some manipulation to this list. What I want is I want to group by this list elements by two columns and filter them with a certain value.

So I know how to group by by two colums such as:
C#
myList.GroupBy(g => new{ g.DB, g.Type }).Select(g => g.First())

But here while group by I want the Type equals to "C"
I dont know but my goal is to code something like:
C#
myList.GroupBy(g => new{ g.DB, g.Type.Equals("C") }).Select(g => g.First())

-OR-
C#
myList.GroupBy(g => new{ g.DB, g.Type }).Where(g => g.Type = "C").Select(g => g.First())


but I can not figure it out. Can someone help me complete this query. Thanks in advance.
Posted

Try this:
C#
myList.GroupBy(g=> new {g.DB, g.Type}).Where(g=>g.Type=="C").FirstOrDefault();
 
Share this answer
 
I believe First is one of the field inside your list.
C#
var newlist=myList.Where(g=>g.Type.Equals("C"))
.GroupBy(g => new{ g.DB, g.Type})
.Select(g => g.First());
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900