Fork me on GitHub
tinylit

Linq语法(一)

Linq语法(一)。

实体一:

public class SimpleDto
{
    public int Id { get; set; }

    public int Code { get; set; }

    public DateTime Date { get; set; }
}

实体二:

public class SimpleRelationDto
{
    public int Id { get; set; }
    public int Code { get; set; }
    public string Name { get; set; }
}

准备:

List<SimpleDto> simples = new List<SimpleDto>(1000);
List<SimpleRelationDto> simpleRelations = new List<SimpleRelationDto>(500);

for (int i = 0; i < 1000; i++)
{
    simples.Add(new SimpleDto
                {
                    Id = i,
                    Code = i % 10,
                    Date = DateTime.Now
                });

    if (i >= 500) continue;

    simpleRelations.Add(new SimpleRelationDto
                        {
                            Id = i,
                            Code = i % 10,
                            Name = $"第{i}位"
                        });
}
  • 常规。

    var linq = from x in simples
                where x.Id > 100 // 筛选条件。
                select x;
    
  • 排序。

    var linq = from x in simples
                orderby x.Code descending, x.Id ascending // 排序方式。
                select x;
    
  • 分组。

    var linq = from x in simples
                where x.Id > 100
                group x by x.Code // 分组依据。
        		into g
                select new
                {
                    Code = g.Key,
                    Count = g.Count()
                };
    
  • 分组统计条件。

    var linq = from x in simples
               where x.Id > 100
               group x by x.Code 
        	   into g
               where g.Count() < 5 // 统计条件。
               select new
               {
                   Code = g.Key,
                   Count = g.Count()
               };
    
  • 连接:默认为内连接。

    var linq = from x in simples
                join y in simpleRelations
                on x.Id equals y.Id // 数据源的关联关系。
                select new
                {
                    x.Id,
                    SimpleCode = x.Code,
                    SimpleRelationCode = y.Code
                };
    
  • 左连接:默认数据。

    var simpleRelation = new SimpleRelationDto { Code = 100 };
    
    var linq = from x in simples // 主源数据。
               join y in simpleRelations // 子源数据。
               on x.Id equals y.Id
               into g
               from y in g.DefaultIfEmpty(simpleRelation) // 在 simpleRelations 中,找不到与 simples 条件关联的数据时,使用此默认数据。
               select new
               {
                   x.Id,
                   SimpleCode = x.Code,
                   SimpleRelationCode = y.Code
               };
    
  • 左连接:默认值。

var linq = from x in simples
           join y in simpleRelations
           on x.Id equals y.Id
            into g
           from y in g.DefaultIfEmpty()
           select new
           {
               x.Id,
               SimpleCode = x.Code,
               SimpleRelationCode = y == null 
                                    ? 10 // 在 simpleRelations 中,找不到与 simples 条件关联的数据时,使用此默认数据。
                                    : y.Code
           };
posted @ 2021-09-30 11:46  影子和树  阅读(41)  评论(0编辑  收藏  举报