[Reprint] C # LINQ to SQL

Source: Internet
Author: User

1,Concat (connecting different sets does not automatically filter the same item. Will delay calculation)

var q = (from c in db.Customers        select c.Phone            ).Concat(            from e in db.Employees            select e.HomePhone);   var q = (from c in db.Customers            select new            {               Name = c.CustomerName,               Phone = c.Phone             }).Concat(            from e in db.Employees            select new            {               Name = e.EmployeeName,               Phone = e.HomePhone             });

 

2,Union (merge to automatically filter identical items. Will delay calculation)

var q = (from c in db.Customers            select c.Country).Union(            from e in db.Employees            select e.Country);

 

3,Intersect (intersection. Will delay calculation)

var q = (from c in db.Customers            select c.Country).Intersect(            from e in db.Employees            select e.Country);
 

4,T (difference, a-B. Exclude A from B from a set. Will delay calculation)

var q = (from c in db.Customers            select c.Country).Except(            from e in db.Employees            select e.Country);

 

5,Top and bottom (fetch a specified amount of data. Will delay calculation)

 

6,Take (obtain the first n data of the set. Will delay calculation)

var q = (from e in db.Employees            orderby e.HireDate            selct e).Take(5);

 

7,Skip (skip the first n data records of the set. Will delay calculation)

var q = (from p in db.Products            orderby p.UnitPrice descending            select p).Skip(10);

Select All products other than the 10 most expensive products

 

8,Takewhile (it is not obtained until a condition is invalid. Will delay calculation)
That is, use its conditions to determine the elements in the source sequence in sequence, and return the elements that meet the judgment conditions. The judgment operation ends at the end of the returned false or source sequence.

 

9,Skipwhile (same as above)

 

10,Paging)

var q = (from c in db.Customers             orderby c.CustomerName             select c).Skip(50).Take(10);

 

11,Like

var q = from c in db.Customers        where SqlMethods.Like(c.CustomerID, "C%")        select c;

Query Consumers whose consumer IDs do not have the "axoxt" format:

var q = from c in db.Customers        where !SqlMethods.Like(c.CustomerID, "A_O_T")        select c;DateDiffDay

Compare two time variables. There are: datediffday, datediffhour, datediffmillisecond, datediffminute, datediffmonth, datediffsecond, datediffyear:

var q = from o in db.Orders        where SqlMethod.DateDiffDay(o.OrderDate, o.ShippedDate) < 10        select o;

Query all orders delivered within 10 days after the order is created

 

12,Compiled query (pre-compiled query)

NorthwindDataContext db = new NorthwindDataContext();    var fn = CompiledQuery.Compile(               (NorthwindDataContext db2, string city) =>              from c in db2.Customers              where c.City == city              select c);    var londonCusts = fn(db, "London");    var seaCusts = fn(db, "Seattle");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.