简体   繁体   中英

how to inner join DataTables using labda expression linq in C#?

I had joined data tables using "Query Syntax", and it is working fine.Could any one help in converting it to lamba statement ?

 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; }
            }

EDIT:

Refer to Queryable.Join Method (IQueryable, IEnumerable, Expression>, Expression>, Expression>) .

The lambda version of your query could be something like this.

Please note that for some properties I just assumed that it comes from 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 joins are ugly. I would not recommend using it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM