zoukankan      html  css  js  c++  java
  • 使用Linq 连接Datatble

    class QuantityTable
    {
    public string NAME { get; set; }
    public string T { get; set; }
    public string UNIT { get; set; }
    public decimal DAYWQUAN { get; set; }
    public decimal WEEKQUAN { get; set; }
    public decimal WEEKWQUAN { get; set; }
    public decimal WEEKLV { get; set; }
    public decimal MONTHQUAN { get; set; }
    public decimal MONTHWQUAN { get; set; }
    public decimal MONTHLV { get; set; }
    public decimal YEARQUAN { get; set; }
    public decimal YEARWQUAN { get; set; }
    public decimal YEARLV { get; set; }
    }

    DataTable zdt=GetDatatable();

    DataTable dt4 = zdt.AsEnumerable().Where(z => z.Field<decimal>("TYPE") == 4 && z["WORK_RESOURCE_NO"] != DBNull.Value).CopyToDataTable<DataRow>();
    DataTable dt3 = zdt.AsEnumerable().Where(z => z.Field<decimal>("TYPE") == 3 && z["WORK_RESOURCE_NO"] != DBNull.Value).CopyToDataTable<DataRow>();
    DataTable dt2 = zdt.AsEnumerable().Where(z => z.Field<decimal>("TYPE") == 2 && z["WORK_RESOURCE_NO"] != DBNull.Value).CopyToDataTable<DataRow>();
    DataTable dt1 = zdt.AsEnumerable().Where(z => z.Field<decimal>("TYPE") == 1 && z["WORK_RESOURCE_NO"] != DBNull.Value).CopyToDataTable<DataRow>();
    DataTable swDt = DbUtil.QueryDT("select work_resource_no, name, CUSTOMER_COL_9 from s_work_resourse");

    quantityTables = (from dr4 in dt4.AsEnumerable()
    join dr3 in dt3.AsEnumerable()
    on new
    {
    t = dr4.Field<string>("T"),
    sn = dr4.Field<decimal>("SN"),
    unit = dr4.Field<string>("UNIT"),
    work_resource_no = dr4.Field<decimal>("WORK_RESOURCE_NO")
    } equals new
    {
    t = dr3.Field<string>("T"),
    sn = dr3.Field<decimal>("SN"),
    unit = dr3.Field<string>("UNIT"),
    work_resource_no = dr3.Field<decimal>("WORK_RESOURCE_NO")
    } into result1
    from r1 in result1.DefaultIfEmpty()
    join dr2 in dt2.AsEnumerable()
    on new
    {
    t = dr4.Field<string>("T"),
    sn = dr4.Field<decimal>("SN"),
    unit = dr4.Field<string>("UNIT"),
    work_resource_no = dr4.Field<decimal>("WORK_RESOURCE_NO")
    } equals new
    {
    t = dr2.Field<string>("T"),
    sn = dr2.Field<decimal>("SN"),
    unit = dr2.Field<string>("UNIT"),
    work_resource_no = dr2.Field<decimal>("WORK_RESOURCE_NO")
    } into result2
    from r2 in result2.DefaultIfEmpty()
    join dr1 in dt1.AsEnumerable()
    on new
    {
    t = dr4.Field<string>("T"),
    sn = dr4.Field<decimal>("SN"),
    unit = dr4.Field<string>("UNIT"),
    work_resource_no = dr4.Field<decimal>("WORK_RESOURCE_NO")
    } equals new
    {
    t = dr1.Field<string>("T"),
    sn = dr1.Field<decimal>("SN"),
    unit = dr1.Field<string>("UNIT"),
    work_resource_no = dr1.Field<decimal>("WORK_RESOURCE_NO")
    } into result3
    from r3 in result3.DefaultIfEmpty()
    join sw in swDt.AsEnumerable()
    on new
    {
    work_resource_no = dr4.Field<decimal>("WORK_RESOURCE_NO")
    } equals new
    {
    work_resource_no = sw.Field<decimal>("WORK_RESOURCE_NO")
    } into result4
    from r4 in result4.DefaultIfEmpty()
    where r4 != null && r4.Field<string>("NAME") != null
    orderby r4.Field<string>("CUSTOMER_COL_9"), dr4.Field<decimal>("SN")
    select new QuantityTable
    {
    NAME = r4.Field<string>("NAME"),
    T = dr4.Field<string>("T"),
    UNIT = dr4.Field<string>("UNIT"),
    DAYWQUAN = r3?.Field<decimal>("WQUAN") ?? 0,//r3!=null 返回r3.Field<decimal>("WQUAN") 反之返回0
    WEEKQUAN = r2?.Field<decimal>("QUAN") ?? 0,
    WEEKWQUAN = r2?.Field<decimal>("WQUAN") ?? 0,
    WEEKLV = r2 == null ? 0 : Math.Round(r2.Field<decimal>("WQUAN") / r2.Field<decimal>("QUAN"), 4, MidpointRounding.AwayFromZero) * 100,
    MONTHQUAN = r1?.Field<decimal>("QUAN") ?? 0,
    MONTHWQUAN = r1?.Field<decimal>("WQUAN") ?? 0,
    MONTHLV = r1 == null ? 0 : Math.Round(r1.Field<decimal>("WQUAN") / r1.Field<decimal>("QUAN"), 4, MidpointRounding.AwayFromZero) * 100,
    YEARQUAN = dr4?.Field<decimal>("QUAN") ?? 0,
    YEARWQUAN = dr4?.Field<decimal>("WQUAN") ?? 0,
    YEARLV = dr4 == null ? 0 : Math.Round(dr4.Field<decimal>("WQUAN") / dr4.Field<decimal>("QUAN"), 4, MidpointRounding.AwayFromZero) * 100,

    }).ToList();

    换成sql的话:

    select  swDt .name,dt4 .T, dt4 .UNIT (省略..)  from dt4 

     left join dt3 on (省略..)  

     left join dt2 on (省略..)  

     left join dt1 on  (省略..)   

     left join swDt on  (省略..)  

    where swDt .name is not null 

    order by swDt .CUSTOMER_COL_9,dt4 .SN

    就这么个意思,随便记录一下

  • 相关阅读:
    git
    uniapp
    laravel
    laravel
    js
    js -上传文件获取名字赋值
    laravel
    持续集成
    持续集成
    持续集成
  • 原文地址:https://www.cnblogs.com/LDJW/p/15687369.html
Copyright © 2011-2022 走看看