
Usage of C# (C Sharp) Query Keywords group, by, into When you
are framing queries using LINQ Query Expression there may be situations
when you want to group records based on a specific criteria or a specific
field. This is much alike GROUP BY clause of SQL.
You can perform
grouping in C# query expressions using the keywords group and by. You
will understand more about these two keywords in this article. In addition,
you will know how to store the result of grouping in a temporary variable
to process it further in rest of the query. This is achieved using into
keyword. Assume that
you have a class to record employee details including his supervisor id.
What if you want to list the employees working with each supervisor? You
can easily do it using group and by keywords as shown in the example below: class sampleClass
{ public static
List<employee> accessEmployeeRecords() { Output of
this code will be: Supervisor
with ID 110 supervises 3 employee(s) whose details are as below: In this example
you have displayed the count pertaining to number of employees working
with each supervisor and you have also provided details about each employee
working with each supervisor. If you look at the query, you might be surprised
to see that there is no select clause in the query. Then how are the details
fetched and displayed? The result is fetched after the group keyword and
before by keyword. After by keyword you specify the criteria or the field
based on which grouping has to happen. In the query
highlighted above, you have specified empRec after group keyword which
means that you are returning the whole employee record as the result.
After by keyword you have specified empRec.supervisorId which means that
you are grouping the records based on supervisorId field of each employee
instance. Hence the
key for grouping is the supervisorId and for each supervisor you will
have one or more employee records associated. This information is retrieved
in the foreach iteration statement. Since for every supervisor the corresponding
employee records might be more than one, inside foreach statement iterating
each supervisor you have another foreach statement iterating employee
records for that supervisor. You have
returned the entire employee record in this query. What if you want to
display only the empId field of each employee and empName field is not
required? In that case you can rephrase the query as: var groupQuery
=from empRec in emp As already
discussed, the examples you have seen so far doesnt have a select
clause because the group clause in itself fetches the result. Hence select
clause is not required. However if you want to perform further processing
based on the result fetched from the group clause, then you have to use
select clause. For example if you want to sort the results of earlier
query based on supervisorId, then your query will be: var groupQuery
=from empRec in emp To sort the
result, you need to store the grouping result into a temporary variable.
That is done using the query keyword into. Using this keyword you have
stored the result of grouping inside a variable called grpResult. You
use this variable for further processing. To sort the result based on
supervisorId you have used orderby clause and you have fetched the supervisorId
using grpResult.Key where Key refers to the key field based on which grouping
is done. Sorted records
are then fetched using the select clause. Here again, instead of fetching
entire result of grouping (grpResult) in select clause you can fetch specific
information alone. For example if you want to retrieve only the supervisorId
and number of employees working with the supervisor and you dont
require details of the employees working with each supervisor, then you
can reframe the query as: var groupQuery
=from empRec in emp Ensure the
following guidelines in your query: No
other clauses other than select clause and group clause can be at the
end of your query i.e. the last clause in your query So far you
have grouped the records based on a single field. You can group records
using more than one field as well. In such case, those fields are together
termed as composite keys. Assume that the class employee includes one
more field called supervisorName. You can group the records using both
supervisorId as well as supervisorName. For that, rephrase the query as
shown below: In all the
examples discussed above, after by keyword you have specified a field.
You can also specify a condition based on which grouping has to happen.
Here is an example: Here the
grouping condition is empRec.empId > 122 which means that the output
will contain two groups, one group containing employees with empId less
than or equal to 122 and the other group containing employees with empId
greater than 122.
_______________________________________________________________________
FREE
Subscription
Subscribe
to our mailing list and receive new articles Note
: We never rent, trade, or sell my email lists to Visit
.NET Programming Tutorial Homepage ______________________________________________________ |