Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
List<int> obj = 
          datacontext.tblAdverts
          .Where(i => i.BusinessUserId == id)
          .GroupBy(i => i.Plans)
          .Select(i => i.Key.Value).ToList();
Posted
Updated 3-Dec-14 18:38pm
v2

List<int> obj = datacontext.tblAdverts

// build Collection#1: create a collection of all entries in tblAdverts where the entry's BusinessUserId matches the value of the variable 'id

.Where(i => i.BusinessUserId == id)

// build Collection#2: analyze Collection#1 to build a Collection of Groups, where each Group (a sequence) contains those items in Collection#1 whose inner object 'Plans is identical.

.GroupBy(i => i.Plans)

// build Collection#3: go through each Group in Collection#2 and get the Key.Value integer property, add it to Collection#3

.Select(i => i.Key.Value)

// evaluate/render/process the temporary structures created by the internal Linq operators into a List of Type 'int

.ToList();
 
Share this answer
 
Comments
TheRealSteveJudge 4-Dec-14 10:05am    
5*
Maciej Los 4-Dec-14 11:50am    
5ed!
BillWoodruff 4-Dec-14 22:27pm    
Thank you !
you are selecting from group by results, Key means here based on what you have done the grouping. ( here Plans), so you selecting Plans.Value property of each group results. Here value may be a property or if it is a Nullable type then most probable it is Nullable<t>.Value Property [^]
 
Share this answer
 
This is called LINQ(Language Integrated Queries), with the help of which which at the back end this gets converted to normal Sql queries which you can also check using a very nice tool called LINQPAD[^]
Here in this statement you have mentioned, this query
returns a list of integers from the Table named tblAdvert with the help of its context i.e. tblAdverts(generally context names are pluralized), where a filter is added based on the BusinessUserId and Grouped by plans column and then select the key.
This iswhat in general this linq means.
But I am unaware of why you have used key.Value here.
I hope I could make you understand somehow.
Post back your queries if any.
Thanks.
 
Share this answer
 
Select will return the object(List<int>) which is specified the LINQ condition.
 
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