/// <summary>
/// Select - Select选择;延迟
/// </summary>
private void Summary_Select()
{
var ary = _ctx.Categories.Select(
c => new { CategoryName = "类别名称:" + c.CategoryName });
foreach (var c in ary)
result.InnerHtml += c.CategoryName + "<br />";
var ary2 = from c in _ctx.Categories
select new { CategoryName = "类别名称:" + c.CategoryName };
foreach (var c in ary2)
result.InnerHtml += c.CategoryName + "<br />";
result.InnerHtml += "<br />";
}
/// <summary>
/// Where - Where查询;延迟
/// </summary>
private void Summary_Where()
{
var categories = _ary.Where(
a => a.StartsWith("w") && a.EndsWith("f"));
foreach (var a in categories)
result.InnerHtml += a+"<br />";
var categories2 = from a in _ary
where a.StartsWith("w") && a.EndsWith("f")
select a;
foreach (var a in categories2)
result.InnerHtml += a + "<br />";
result.InnerHtml += "<br />";
}
/// <summary>
/// OrderBy - 按指定表达式对集合正序排序;延迟
/// OrderByDescending - 按指定表达式对集合倒序排序;延迟
/// </summary>
private void Summary_OrderByDescending()
{
var ary = (from a in _ary
select a).OrderByDescending(a => a.Length);
foreach (var a in ary)
result.InnerHtml += a + "<br />";
var ary2 = from a in _ary
orderby a.Length descending
select a;
foreach (var a in ary2)
result.InnerHtml += a + "<br />";
result.InnerHtml += "<br />";
}
/// <summary>
/// GroupBy_OrderBy - 分组 + 排序;延迟
/// </summary>
private void Summary_GroupBy_OrderBy()
{
var list = (from a in _ary
select a).GroupBy(a => a.Length).Select(
g => new { Group = g.Key, Member = g }).OrderBy(a => a.Group);
foreach (var g in list)
{
result.InnerHtml += g.Group + "个字符<br />" ;
foreach (string m in g.Member)
result.InnerHtml += "--" + m + "<br />";
result.InnerHtml += "<br />";
}
var list2 = from a in _ary
group a by a.Length into g
orderby g.Key
select new { Group = g.Key, Member = g };
foreach (var g in list2)
{
result.InnerHtml += g.Group + "个字符<br />";
foreach (string m in g.Member)
result.InnerHtml += "--" + m + "<br />";
result.InnerHtml += "<br />";
}
result.InnerHtml += "<br />";
}
/// <summary>
/// Join - Join查询;延迟
/// </summary>
private void Summary_Join()
{
var products = _ctx.Products.Join(
_ctx.Categories,
p => p.CategoryID,
c => c.CategoryID,
(p, c) => new { c.CategoryName, p.ProductName }).Take(5);
foreach (var p in products)
result.InnerHtml +=p.CategoryName +"-"+p.ProductName+ "<br />";
result.InnerHtml += "<br />";
var products2 = (from p in _ctx.Products
join c in _ctx.Categories on p.CategoryID equals c.CategoryID
select new { c.CategoryName, p.ProductName }).Take(5);
foreach (var p in products2)
result.InnerHtml += p.CategoryName + "-" + p.ProductName + "<br />";
result.InnerHtml += "<br />";
}
/// <summary>
/// GroupJoin - 分组Join查询;延迟
/// </summary>
private void Summary_GroupJoin()
{
var categories = _ctx.Categories.GroupJoin(
_ctx.Products,
c => c.CategoryID,
p => p.CategoryID,
(c, g) => new { c.CategoryName, ProductCount = g.Count() });
foreach (var c in categories)
result.InnerHtml += c.CategoryName + "-" + c.ProductCount;
result.InnerHtml += "<br />";
var categories2 = from c in _ctx.Categories
join p in _ctx.Products on c.CategoryID equals p.CategoryID into g
select new { c.CategoryName, ProductCount = g.Count() };
foreach (var c in categories2)
result.InnerHtml += c.CategoryName + "-" + c.ProductCount;
result.InnerHtml += "<br />";
}
示例代码:
https://files.cnblogs.com/hyosft/Linq%e6%9f%a5%e8%af%a2%e6%93%8d%e4%bd%9c%e7%ac%a6%e7%a4%ba%e4%be%8b.rar