.net lambad expression operator database

Mainly in the common database operations by re-expressed under the LAMBDA expression, not much use, but relatively common, and so there will be a time extension, and LINQ to query and also together when the next refresh:

1.select statement: books.Select (p => new {p.Title, p.UnitPrice, p.Author}); // required anonymity

2.where语句:books.Where(p=>p.UnitPrice==100&&p.Title=”ABC”);

supplement:

Image database LIKE '% c ++%', LAMBDA by p.Title.Contains ( "c ++") represents;

Image database LIKE 'c%', LAMBDA by p.Title.StartWith ( "c") represents;

Image database LIKE '% c', LAMBDA by p.Title.EndsWith ( "c") represents;

Where another form of expression:

books.Where(p=>{

    var ret = p.UnitPrice>30&&p.Title.Contains(“c++”);

    Return the right;

});

3. Sort statement:

Like the database in ascending order by:

By "subject .OrderBy (p => p.UnitPrice)" implemented

Like database descending order by:

By "subject .OrderByDescending (p => p.UnitPrice)" implemented

Like database order by UnitPrice desc, Title asc:

By "subject .OrderByDescending (p => p.UnitPrice) .ThenBy (p => p.Title)"

In turn, it is: "Object .OrderBy (p => p.UnitPrice) .ThenByDescending (p => p.Title)"

 

4. The set of functions:

  var max = books.Where(p => p.CategoryId == 1001).Max(p => p.UnitPrice);

        var min = books.Min(p => p.UnitPrice);

        var count = books.Count( );

        var avg = books.Average(p => p.UnitPrice);

        var sum = books.Sum(p => p.UnitPrice);

Note that the above these things get, not the object, a single value

 

5. GROUP BY function

// select categoryid,max(unitpirce) from books group by categoryid having max(unitprice)>50

        var list6 = books.GroupBy(p => p.CategoryId).Where(p=>p.Max(q=>q.UnitPrice)>50);

        foreach (var item in list6)

        { 

            Response.Write(string.Format("

  • Category ID: {0}, {1} highest price
  • ",

item.Key,item.Max(p=>p.UnitPrice)));

        }

 

 

 

 

6. TOP function

// get a range such as 3,5

var list7 = books.Skip(2).Take(3).Select(p => new { p.Title, p.CategoryId,  p.UnitPrice });

// select top 5 

var list7 = books.Take(5).OrderByDescending(p => p.UnitPrice)

 .Select(p => new { p.CategoryId, p.UnitPrice, p.Title, p.Author });

 

7.union function

books.Where(p => p.CategoryId == 1001).Select(p => new { p.CategoryId, p.UnitPrice, p.Title, p.Author }).Union(books.Where(p => p.CategoryId == 1002).Select(p => new { p.CategoryId, p.UnitPrice, p.Title, p.Author }));

Select clause to be listed here correspond with the database is the same

 

8.Join method for implementing the two-table join query the database

//select a.title,a.unitprice,a.categoryid,b.id,b.name from books a,category b 

// where a.categoryid = b.id and b.name = 'database'

books.Join(cates.Where(m => m.Name == "数据库"),p => p.CategoryId, q => q.ID, (a, b) => new { a.Title, a.UnitPrice, a.CategoryId, b.ID, b.Name });

Description:

Call the object Join () method is similar to table names in SQL statements in the first table

The first parameter and the Join () method is the second condition table entry name Where

The second and third parameters the Join () method respectively associated with the first field and the second table of the table

The fourth parameter the Join () method expressed the need to obtain from the two fields in the table, (a, b) are tables showing the first and second tables

Guess you like

Origin www.cnblogs.com/yjm8023/p/11161931.html