Write the
below code and execute
<%@ Page
Language="C#"
AutoEventWireup="true"
CodeBehind="LinqAndLambdaExpressions.aspx.cs"
Inherits="SampleTestingCodes.LinqAndLambdaExpressions"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1"
runat="server"
AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="UserId"
/>
<asp:BoundField DataField="UserName"
/>
<asp:BoundField DataField="Education"
/>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace SampleTestingCodes
{
public partial class LinqAndLambdaExpressions
: System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
DataTable dt = new
DataTable();
dt.Columns.Add("UserId", typeof(int));
dt.Columns.Add("UserName", typeof(string));
dt.Columns.Add("Education",
typeof(string));
dt.Rows.Add(1, "Lokesh", "B.Tech");
dt.Rows.Add(2, "Vijay", "Msc");
dt.Rows.Add(3, "Balaji", "MS");
dt.Rows.Add(10, "Charan", "B.Tech");
dt.Rows.Add(5, "Durga", "MD");
dt.Rows.Add(6, "Shiva", "B.Tech");
dt.Rows.Add(20, "Venkat", "CA");
//Get the Data from the data table based on the condition
var dsWhere = dt.AsEnumerable().Where(s =>
s.Field<int>("UserId")
>= 1);
var dsWhere1 = (from
myRow in dt
.AsEnumerable()
where myRow.Field<int>("UserId") >= 5
select
myRow).CopyToDataTable();
var UserId = dt.AsEnumerable().Select(s => s["UserId"]);
//Using Aggregate Method in the LAMBDA Expression
var AdditionOfAllNumbersUsingAggregate =
UserId.Aggregate((FirstValue, NextValue) => Convert.ToInt16(FirstValue)
+ Convert.ToInt16(NextValue));
//Oupput:- 47
//Find the greatest Number in the array
var FindGreatestNumberUsingAggregate = UserId.Aggregate((FirstValue,
NextValue) => Convert.ToInt16(NextValue)
> Convert.ToInt16(FirstValue) ? NextValue
: FirstValue);
//Output:- 20
dt = new DataTable();
dt.Columns.Add("UserId", typeof(int));
dt.Columns.Add("UserName", typeof(string));
dt.Columns.Add("Education",
typeof(string));
dt.Rows.Add(1, "Lokesh", "B.Tech");
dt.Rows.Add(1, "Lava", "B.Tech");
//
bool ConditionAll = dt.AsEnumerable().All(s =>
s.Field<string>("UserName").Contains("L"));
// Returns True
dt = new DataTable();
dt.Columns.Add("UserId", typeof(int));
dt.Columns.Add("UserName", typeof(string));
dt.Columns.Add("Education",
typeof(string));
dt.Rows.Add(1, "Lokesh", "B.Tech");
dt.Rows.Add(1, "Bava", "B.Tech");
//It will works for all having the specific letter
bool All = dt.AsEnumerable().All(s => s.Field<string>("UserName").Contains("L"));
// Returns False all usernames not contains L
//It identifies any of the word contains with this letter
returns true
bool Any = dt.AsEnumerable().Any(s => s.Field<string>("UserName").Contains("L"));
//Returns True
//--------------------------------------------------------------------------------------------
DataTable dt1 = new
DataTable();
dt1.Columns.Add("UserId", typeof(int));
dt1.Columns.Add("UserName",
typeof(string));
dt1.Columns.Add("Education",
typeof(string));
dt1.Rows.Add(1, "Lokesh", "B.Tech");
dt1.Rows.Add(2, "Vijay", "Msc");
dt1.Rows.Add(3, "Balaji", "MS");
dt1.Rows.Add(10, "Charan", "B.Tech");
dt1.Rows.Add(5, "Durga", "MD");
dt1.Rows.Add(6, "Shiva", "B.Tech");
dt1.Rows.Add(20, "Venkat", "CA");
var DataViewData = dt1.AsEnumerable().Where(s =>
s.Field<int>("UserId")
>= 4);
GridView1.DataSource = DataViewData.AsDataView();
GridView1.DataBind();
//=====
//Example
//-======
//Gives the average of the column data
var DataAverage = dt1.AsEnumerable().Average(s =>
s.Field<int>("UserId"));
//OutPut:-
6.7142857142857144
///It will join the
esisting list with the sorted list
var DataConcat =
dt1.AsEnumerable().Concat(dt1.AsEnumerable().Where(s => s.Field<int>("UserId")
>= 10));
//=====
//Example Distinct()
//-======
DataTable dt2 = new
DataTable();
dt2.Columns.Add("UserId", typeof(int));
dt2.Columns.Add("UserName",
typeof(string));
dt2.Columns.Add("Education",
typeof(string));
dt2.Rows.Add(1, "Lokesh", "B.Tech");
dt2.Rows.Add(2, "Vijay", "Msc");
dt2.Rows.Add(3, "Lokesh", "MS");
dt2.Rows.Add(10, "Charan", "B.Tech");
var DataDistinct = dt2.AsEnumerable().Select(s =>
s.Field<string>("UserName")).Distinct();
//output:-
//[0] = "Lokesh"
//[1] =
"Vijay"
//[2] = "Charan"
var DataElementAt =
dt2.AsEnumerable().ElementAt(0).Field<string>("UserName");
//output:-
//DataElementAt "Lokesh"
var DataElementAtOrDefault = dt2.AsEnumerable().ElementAtOrDefault(0).Field<string>("UserName");
var DataFirst = dt2.AsEnumerable().First(s =>
s.Field<int>("UserId")
> 2);
var DataLast = dt2.AsEnumerable().Last(s =>
s.Field<int>("UserId")
> 2);
var DataGroupBy = dt2.AsEnumerable().Select(s => new { UserName = s.Field<string>("UserName"), Education = s.Field<string>("Education")
}).GroupBy(s => s.UserName).Select(s => new
{ Name = s.Key, Count = s.Select(t => t.Education).Count(), Educations =
s.Select(t => t.Education).ToArray() });
var DataLongCount = dt2.AsEnumerable().LongCount();//It returns Int64 bit count
var DataCount = dt2.AsEnumerable().Count();//It returns only int count
//=====
//Example OfType()
//-======
DataTable dt3 = new
DataTable();
dt3.Columns.Add("UserId", typeof(int));
dt3.Columns.Add("UserName",
typeof(string));
dt3.Columns.Add("Education",
typeof(string));
dt3.Columns.Add("Comments",
typeof(object));
dt3.Rows.Add(1, "Lokesh", "B.Tech",234);
dt3.Rows.Add(15, "Vijay", "Msc","good");
dt3.Rows.Add(3, "Lokesh", "MS","Raghu
master");
dt3.Rows.Add(10, "Charan", "B.Tech",100);
var DataOfType = dt3.AsEnumerable().Select(s =>
s.Field<object>("Comments")).OfType<int>();
var DataOfTypeString = dt3.AsEnumerable().Select(s
=> s.Field<object>("Comments")).OfType<string>();
var DataOrderBy = dt3.AsEnumerable().OrderBy(s =>
s.Field<string>("UserName")).CopyToDataTable();
DataTable dtt = new
DataTable();
dtt = DataOrderBy;
var DataOrderByDescending =
dt3.AsEnumerable().OrderByDescending(s => s.Field<string>("UserName")).CopyToDataTable();
var DataReverse = dt3.AsEnumerable().Select(s =>
s.Field<string>("UserName")).Reverse();
var DataReverse1 = dt3.AsEnumerable().Select(s =>
s.Field<int>("UserId")).Reverse();
//Skip will works as
//skip the collection from starting to count number of
records
var DataSkip = dt3.AsEnumerable().Select(s => s.Field<int>("UserId")).Skip(1);
var DataToDictionary =
dt3.AsEnumerable().ToDictionary(s => s.Field<int>("UserId"));
}
}
}
Happy Coding JJJJJJJJJJJJJJJJJJJJJJJJJ
No comments:
Post a Comment