SIGN UP MEMBER LOGIN:    
ARTICLE

Linq queries for beginners

Posted by Ravi Panara Articles | LINQ with C# December 01, 2010
Following are some of the basic Linq command/samples for those who just want to start learning Linq.
Reader Level:

Following are some of the basic Linq command/samples for those who just want to start learning Linq. So this will help a lot to all the beginners to easily understand Linq queries.

// Basic Query
// To achieve  Select * From ProductMst  you need to write follofing linq Query
  var Result1 = from p in db.ProductMst
                 select p;
// To achieve  Select ProductID, ProductName, Price From ProductMst you need to write follofing linq Query
    var Result2 = from p in db.ProductMst
                 select new {
                     p.ProductID,
                     p.ProductName,
                     p.Price
                 };

// Where Clause Query
// To achieve  Select * From ProductMst Where ProductID = 1 you need to write follofing linq Query
    var Result3 = from p in db.ProductMst
                 where p.ProductID == 1
                 select p;

// To achieve Select * From ProductMst Where SupplierId =2 and Price > 10 you need to write follofing linq Query
    var Result4 = from p in db.ProductMst
                 where p.SupplierID == 2 && p.Price > 10
                 select p;


// To achieve Select * From ProductMst Where SupplierId =2 Or SupplierId=5 you need to write follofing linq Query
    var Result5 = from p in db.ProductMst
                 where p.SupplierID == 2 || p.SupplierID == 5
                 select p;

// Order By Query
//To achieve   Select * From ProductMst Order By ProductId you need to write follofing linq Query
    var Result6 = from p in db.ProductMst
                 orderby p.ProductID
                 select p;

// To achieve  Select * From ProductMst Order By ProductId Desc you need to write follofing linq Query
    var Result7 = from p in db.ProductMst
                 orderby p.ProductID descending
                 select p;

// To achieve  Select * From ProductMst Order By CategoryId, Price Desc you need to write follofing linq Query
    var Result8 = from p in db.ProductMst
                 orderby p.CategoryID, p.Price descending
                 select p;

// Top Query
//To achieve Select Top 5 * From ProductMst you need to write follofing linq Query
    var Result9 = (from p in db.ProductMst
                 select p).Take(5);

//To achieve Select Top 1 * From ProductMst you need to write follofing linq Query
    var Result10 = (from p in db.ProductMst
                   select p).Take(1);
     or

    var Result11 = (from p in db.ProductMst
                   select p).First();

// Distinct Query
//To achieve Select Distinct CategoryId From ProductMst you need to write follofing linq Query

    var Result13 = (from p in db.ProductMst
                   select p.CategoryID).Distinct();

// Group By Query
//To achieve Select CategoryId, Count(CategoryID) As FieldName From ProductMst Group By CategoryId you need to write follofing linq Query
    var Result14 = from p in db.ProductMst
                  group p by p.CategoryID into g
                  select new {
                      CategoryId = g.Key,
                      FieldName = g.Count()
                  };

//To achieve Select CategoryId, Avg(UnitPrice) As NewField From ProductMst Group By CategoryId you need to write follofing linq Query
    var Result15 = from p in db.ProductMst
                  group p by p.CategoryID into g
                  select new {
                      CategoryId = g.Key,
                      FieldName = g.Average(S => S.Price)
                  };

// Union Query
//To achieve Select * From ProductMst Where CategoryId =1 union Select *  From ProductMst Where CategoryId = 2 you need to write follofing linq Query
    var Result17 = (from p in db.ProductMst
                   where p.CategoryID == 1
                   select p).Union(
                       from m in db.ProductMst
                       where m.CategoryID == 2
                       select m
                   );

Login to add your contents and source code to this article
share this article :
post comment
 

Thank sir for giving this information..

Posted by Manohar Medarametla Dec 03, 2010

Hi,
please modify following query according your requirement.

from p in PartTable
join j in CustTable on p.cust_id equals j.id
where j.cust_name == "abc"
select p

you can replace "abc" with the variable you have set for the name.

Thanks,

Posted by Ravi Panara Dec 03, 2010

Would be good if you can post how to query data from more than one table. I know there is join but is there a simpler method to do this ?

Posted by Mithun C Dec 02, 2010

If you assume the input variable sCustName as a string, how would you write the following...

SELECT name FROM PartTable WHERE cust_id = (SELECT id FROM CustTable WHERE cust_name = sCustName);

Posted by Jim Middleton Dec 02, 2010

I'm new to C#/ASP.net and this is precisily what i was looking for.

Posted by Jim Middleton Dec 02, 2010
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor