zoukankan      html  css  js  c++  java
  • Linq

    Linq的连接方式

    var ArgStr = " [{"ID":1,"Age":"18" },{"ID":2,"Age":"19" },{"ID":3,"Age":"20" }] ";
    var NameStr = " [{"ID":1,"Name":"小红" },{"ID":3,"Name":"小白" },{"ID":4,"Name":"小王" }] ";

    JavaScriptSerializer Serializer = new JavaScriptSerializer();
    List<ID_Age> LeftArray = Serializer.Deserialize<List<ID_Age>>(ArgStr);

    List<ID_Name> RightArray = Serializer.Deserialize<List<ID_Name>>(NameStr);


    //1、左连接:
    var LeftJoin = from a in LeftArray
    join b in RightArray on a.ID equals b.ID into NewArray
    from b in NewArray.DefaultIfEmpty()
    select new
    {
    ID = a.ID,
    Name = b != null ? b.Name : null,
    Age = a.Age
    };

    //2、右连接:
    var RightJoin = from a in RightArray
    join b in LeftArray on a.ID equals b.ID into NewArray
    from b in NewArray.DefaultIfEmpty()
    select new
    {
    ID = a.ID,
    Name = a.Name,
    Age = b != null ? b.Age : null
    };

    //3、内连接:
    var InnerJoin = from a in LeftArray
    join b in RightArray on a.ID equals b.ID
    select new
    {
    ID = a.ID,
    Name = b != null ? b.Name : null,
    Age = a.Age
    };

     

     Linq  DataTable

    var temparray = from a in temp
    join b in dt_Points.AsEnumerable() on a.TagID equals b.Field<long>("TagID ")
    select new Record
    {
    AddValue = a.AddValue,
    DevUnit = b.Field<string>("DevUnit"),
    TagConfigCode = b.Field<string>("TagConfigCode"),
    TagID = Convert.ToInt64(a.TagID),
    TagName = a.TagName,
    Used = a.Used,
    RecordTime = a.DateTime
    };

    Linq  group

    单表的group

    var SimpleTemp= from a in List1
    group t by new
    {
    t.ID,t.Name
    } into a
    select new
    {

    Score= a.Sum(x => x.Score),
    ID=  a.Key.ID,
    Name= a.Key.Name,
    };

    复合表的group

    var MultTemp= from a in Lst1
    join b in Lst2 on Lst1.ID equals Lst2.ID
    group new
    {
    ID = a.ID ,
    Name= a.Name,
    Score= b.Score
    } by new
    {
    ID = a.ID ,
    Name= a.Name,
    } into t
    select new
    {
    ID = t.Key.ID ,
    Name= t.Key.Name,
    AllScore= t.Sum(s => s.Score),
    };

  • 相关阅读:
    路由器DHCP 动态主机配置
    nat转换
    静态路由的实验
    不同vlan之间的相互访问
    IP的包头格式什么?请分析每个字段的含义
    IP地址的分类
    关于对JSON.parse()与JSON.stringify()的理解
    Ajax工作原理
    关于css伪类
    开发 | 如何在微信小程序的页面间传递数据?
  • 原文地址:https://www.cnblogs.com/NinaMua/p/11549644.html
Copyright © 2011-2022 走看看