如何在C#中使用labda表达式linq连接DataTables?

时间:2017-05-04 07:40:34

标签: c# linq linq-to-dataset

我使用"查询语法"加入了数据表,它运行正常。可以帮助将它转换为lamba语句吗?

 var oVarLoanDetails = (from data in ActiveReferralDetails.AsEnumerable()
    join reftypedata in ActionType.AsEnumerable()
    on data.Field<int>("atid") equals reftypedata.Field<int>("atid")
    join refsubtypedata in ActionSubType.AsEnumerable()
    on data.Field<int>("ASTID") equals refsubtypedata.Field<int>("ASTID")
    where reftypedata.Field<int>("atid") == refsubtypedata.Field<int>("atid")
     select new LoanDataInfo
             {
            LoanNumber = data.Field<string>("Loanno").ToLower(),
            ATID = reftypedata.Field<int>("atid"),
            RefType = reftypedata.Field<string>("ATSHORTDESC"),
            RefSubType = refsubtypedata.Field<string>("SUBTYPESHORTDESC")
             });
        //Class LoanDataInfo

        public class LoanDataInfo
            {
                public string LoanNumber { get; set; }
                public int ATID { get; set; }
                public string RefType { get; set; }
                public string RefSubType { get; set; }
            }

1 个答案:

答案 0 :(得分:0)

编辑:

请参阅Queryable.Join Method (IQueryable, IEnumerable, Expression>, Expression>, Expression>)

您的查询的lambda版本可能是这样的。

请注意,对于某些属性,我只是假设它来自ActiveReferralDetails。

var oVarLoanDetails = ActiveReferralDetails.AsEnumerable()
    .Join(ActionType.AsEnumerable(), 
    ard => arc.Field<int>("atid"), 
    at => at.Field<int>("atid"), 
    (ard, at) => new { ARD = ard, AT = at })
    .Join(ActionSubType.AsEnumerable(), 
    r => new { astid = r.ARD.Field<int>("ASTID"), 
    atid = r.ARD.Field<int>("atid") }, 
    ast => new { astid = ast.Field<int>("ASTID"), 
    atid = ast.Field<int>("atid") },
    (r, ast) => new { ARD = r.ARD, AT = r.AT, AST = ast})
    .Select(r => new LoanDataInfo() {
        LoanNumber = r.ARD.Field<string>("Loanno").ToLower(),
        ATID = r.ARD.Field<int>("atid"),
        RefType = r.ARD.Field<string>("ATSHORTDESC"),
        RefSubType = r.ARD.Field<string>("SUBTYPESHORTDESC")
    });

Lambda加入很难看。我不建议使用它。