Skip to main content

Posts

Showing posts with the label LINQ

Simple LINQ Insert, Update , Delete Tutorial in C#

Open a new Visual Studio windows application project (c#) Right click the project and goto AddNew item . and it will popup the following dialog. select the Linq to SQL Classes and give a neme add click add. then double click the added class in your solution explorer. Then it will load following type of screen. In the Data connection (Assumme you already created a SQL Server Database and also a table nemed "Table1"), Drag and drop the table to the window. Then it will ask for connection string saving dialog. For the moment keep it NO. Now we will see how to use LINQ to do Insert , Update , Delete. Coding is describing below. ( sample is the name given by me to the LinqtoSQL class). private void cmdAdd_Click( object sender, EventArgs e) { sampleDataContext Dt = new sampleDataContext(); Table1 T = new Table1(); T.Address = "Address 1" ; T.Name = "a" ;   Dt.Table1s.InsertOnSubmit(T); Dt.SubmitChanges(); ...

Querying XML using LINQ to XML in C#

< Customers > < Customer id ="1" orderdate ="4/1/1968" > Paul Koch </ Customer > < Customer id ="2" orderdate ="7/5/1988" > Bob Kelly </ Customer > < Customer id ="3" orderdate ="3/24/1990" > Joe Healy </ Customer > </ Customers > Assume this is the XML file you want to search and it is stores in C:\ drive. XDocument myDoc = XDocument .Load( @"C:\sample.xml" ); Ex –1 get the customer who’s having id 2 Method 1 you can use var type variable and through iteration you can get the result. this method is good when you have more than one record. var q = from c in myDoc.Descendants( "Customer" ) where ( int )c.Attribute( "id" ) == 2 select c; Receiving values by iterating through foreach loop.  foreach ( XElement item in q) { String id =item.Attribute( "id" ).Value; String orderdt = item.Attribute( "orderdate"...

Creating and saving XML using LINQ to XML in C#

XML generation is much easier now in C#. You can use System.Xml.Linq namespace to create XML content much faster and easy. lets look at the following XML. < Customers > < Customer id ="1" orderdate ="4/1/1968" > Paul Koch </ Customer > < Customer id ="2" orderdate ="7/5/1988" > Bob Kelly </ Customer > < Customer id ="3" orderdate ="3/24/1990" > Joe Healy </ Customer > </ Customers > Create XML using Linq to XML Following describes how to create the above xml using xElements. XElement xml = new XElement ( "Customers" ); XElement Customer = new XElement ( "Customer" ); Customer.Add( new XAttribute ( "id" ,1)); Customer.Add( new XAttribute ( "orderdate" , "4/1/1968" )); Customer.Value = "Paul Koch" ; xml.Add(Customer); Customer = new XElement ( "Customer" ); Customer.Add( new XAttribute ( "id...

How to use list.Sum<> in System.Collections.Generic

List.Sum<> This will return the summation of named attribute. List<Item> listItem = new List<Item>();   Item T1 = new Item() { Name = "A" , Amount = 100 }; Item T2 = new Item() { Name = "B" , Amount = 600 }; Item T3 = new Item() { Name = "C" , Amount = 100 }; Item T4 = new Item() { Name = "D" , Amount = 150 }; Item T5 = new Item() { Name = "E" , Amount = 200 };   listItem.Add(T1); listItem.Add(T2); listItem.Add(T3); listItem.Add(T4); listItem.Add(T5);   Decimal D = listItem.Sum(P=>P.Amount); // sum of Amount Conditional Sum Assume that you want to get the summation of Amount lager than 100 Decimal D = listItem.Where(P=>P.Amount>100).Sum(P=>P.Amount); List.Sum() This will return the summation of the list. List< int > intList = new List< int >();   intList.Add(2); intList.Add(3); intList.Add(4);   int sum = intList.Sum(); // sum of Amount

How to use list.find<> and list.findall<> in System.Collections.Generic

List.find<> This will return first matched item from the list. List. FindAll<> This will return all matching items from the list. Item Class Code class Item { public int Amount; public string Name; } List<Item> listItem = new List<Item>();   Item T1 = new Item() { Name = "A" , Amount = 100 }; Item T2 = new Item() { Name = "B" , Amount = 600 }; Item T3 = new Item() { Name = "C" , Amount = 100 }; Item T4 = new Item() { Name = "D" , Amount = 150 }; Item T5 = new Item() { Name = "E" , Amount = 200 };   listItem.Add(T1); listItem.Add(T2); listItem.Add(T3); listItem.Add(T4); listItem.Add(T5); Method 1 Item Result= listItem.Find(P=>P.Name== "A" ); // item having "A" as it's item name   Item Result= listItem.Find(P=>(P.Name== "A" & P.Amount==100)); // item having "A" as it's item name and Amount as 100   Item Result= listItem.FindAll(P=>P.Amoun...