Menu ▼


LINQ Aggregate function is used to perform calucalation over sequance/list of values. Below examples will show you how to get SUM, MAX, MIN, COUNT using LINQ Aggregate function.

Example

1. Get sum of numbers in int arrary using LINQ Aggregate function.

int[] intary = { 5, 10, 15, 20 };
int intsum = intary.Aggregate((i, j) => i + j);

Output
//50


2. Get max value usage LINQ Aggregate function.

int[] intary = { 21, 150, 5, 120,80,90 };
int maxval = intary.Aggregate((i, j) => i > j ? i : j);

Output
//150


3. Get min value usage LINQ Aggregate function.

int[] intary = { 21, 150, 5, 120,80,90 };
int minval = intary.Aggregate((i, j) => i < j ? i : j);

Output
//5


4. Get count usage LINQ Aggregate function.

int[] intary = { 21, 150, 5, 120,80,90 };
int intCount = intary.Aggregate(0, (i, j) => i = i + 1);

Output
//6


Read More...
Submited By : mrkraju On 06 Dec 2015

This example shows you how to get the top N records using LINQ query using C#.

Data:

LINQ query to get top 2 records with max invoice amount

var maxinv = (from res in dt.AsEnumerable()
                orderby res.Field<decimal>("Amount") descending
                select new
                {
                    InvoiceID = res.Field<int>("InvoiceID"),
                    Amount = res.Field<string>("InvoiceNumber"),
                    InvoiceNumber = res.Field<decimal>("Amount"),
                    InvoiceDate = res.Field<DateTime>("InvoiceDate")
                }).Take(2);
gvmax.DataSource = maxinv;
gvmax.DataBind();
output:
InvoiceIDInvoiceNumberAmountInvoiceDate
131INV000131400.2514-01-2015 18:25:00
144INV000144351.4519-01-2015 08:10:20

LINQ query to get top 4 records with min invoice amount

var maxinv = (from res in dt.AsEnumerable()
                orderby res.Field<decimal>("Amount") ascending
                select new
                {
                    InvoiceID = res.Field<int>("InvoiceID"),
                    InvoiceNumber = res.Field<string>("InvoiceNumber"),
                    Amount = res.Field<decimal>("Amount"),
                    InvoiceDate = res.Field<DateTime>("InvoiceDate")
                }).Take(4);
gvmin.DataSource = maxinv;
gvmin.DataBind();
Output:
InvoiceIDInvoiceNumberAmountInvoiceDate
125INV00012595.2512-01-2015 10:10:25
158INV000158100.2519-02-2015 14:35:30
164INV000164146.7523-02-2015 12:11:55
159INV000159160.0121-02-2015 15:08:06
Read More...
Submited By : mrkraju On 29 Mar 2015

Language Integrated Query (LINQ), is a component released within the .NET 3.5 Framework. It is one of the most powerful features of .NET 3.5. It serves the purpose of querying objects.

LINQ comprises a series of operators, which are used to query, filter and project data in arrays, enumerable classes, relational databases and XML. In order to query data in LINQ, the data needs to be encapsulated as an object. In case the data source is not an object, it first needs to be converted to an object in order for LINQ to query it.

LINQ has its own Query Processing Engine. The information returned by a LINQ query is a collection of in-memory object which may be enumerated.

The LINQ concept treats the data source as an object, rather than a database. So we may say, its an object that is queried. LINQ may query any type of data source, like:

  • LINQ querying SQL
  • LINQ querying Datasets (also DataTables)
  • LINQ querying ORM Solution
  • LINQ querying Objects (In-memory data may be queried)
  • LING querying XML (Querying is possible on XML data source)

LINQ supports querying to those objects that implements the IEnumerable Interface.

DataContext class represents the main entry point for the LINQ to SQL framework.

The file extension for LINQ to SQL is .dbml 

Note: System.Linq namespace needs to be used for implementing LINQ.

Read More...
Submited By : Krishna Narra On 01 Dec 2013
Here i am trying to explain Linq using Ado.net
Code Snippet:
int result=1;
we are getting datable as result set as a dt.
result set
DataRow resultCodeRow = (from resultCodesTableRow in dt.AsEnumerable()
                                                     where resultCodesTableRow.Field<int?>("Id").Equals(result)
                                                     select resultCodesTableRow).FirstOrDefault();

In the code we have created object for the DataRow as a resultCodeRow and assigning values to the object.

(from resultCodesTableRow in dt.AsEnumerable()
                                                     where resultCodesTableRow.Field<int?>("Id").Equals(result)
                                                     select resultCodesTableRow).FirstOrDefault();
coming to Linq explaination

Here i am creating  resultCodesTableRow as a alias name for the result set "dt" and dt as column name "id" and datatype integer,in front of integer  it mentioned "?" the use this is when there is a null values in column we may get exception. To prevent exception we are using"?". after that we are comparing the condition with the particular resultant value that is "resultId".If satisfies the condition then we are record  in the object using FirstOrDefault(); method.
 
                                      Here the use FirstOrDefault(); method is, it gives first one if there are multiple record with multiplt values.if the value doesn't exists then it gives the default value that is null value.In next Blog i will try to Explain singleorDefault() method
Read More...
Submited By : vinod kumar On 21 Oct 2013




Search dotnetlearners.com