开发者

How to differentiate between two similar fields in Linq Join tables

开发者 https://www.devze.com 2023-01-03 14:56 出处:网络
How to differentiate between two select new fields e.g. Description c.Description and lt.Description DataTable lDt = new DataTable();

How to differentiate between two select new fields e.g. Description

c.Description and lt.Description

    DataTable lDt = new DataTable();
    try
    {

        lDt.Columns.Add(new DataColumn("AreaTypeID", typeof(Int32)));
        lDt.Columns.Add(new DataColumn("CategoryRef", typeof(Int32)));
        lDt.Columns.Add(new DataColumn("Description", typeof(String)));
        lDt.Columns.Add(new DataColumn("CatDescription", typeof(String)));

        EzEagleDBDataContext lDc = new EzEagleDBDataContext();
        var lAreaType = (from lt in lDc.tbl_AreaTypes
                            join c in lDc.tbl_AreaCategories on lt.CategoryRef equals c.CategoryID
                            where lt.AreaTypeID== pTypeId
                            select new { lt.AreaTypeID, lt.Description, lt.CategoryRef, c.Description }).ToArray();

        for (int j = 0; j< lAreaType.Count; j++)
    开发者_Python百科    {
            DataRow dr = lDt.NewRow();
            dr["AreaTypeID"] = lAreaType[j].LandmarkTypeID;
            dr["CategoryRef"] = lAreaType[j].CategoryRef;
            dr["Description"] = lAreaType[j].Description;
            dr["CatDescription"] = lAreaType[j].;
            lDt.Rows.Add(dr);
        }
    }
    catch (Exception ex)
    {
    }


You can give them an explicit name when selecting:

select new { lt.AreaTypeID, LtDescr = lt.Description, lt.CategoryRef, CDescr = c.Description }

And then:

        dr["Description"] = lAreaType[j].LtDescr;
        dr["CatDescription"] = lAreaType[j].CDescr;


Change:

select new { lt.AreaTypeID, lt.Description, lt.CategoryRef, c.Description }

To:

select new { AreaTypeID = lt.AreaTypeID,
      LtDescription = lt.Description,
      CategoryRef = lt.CategoryRef,
      CatDescription = c.Description }

That will give each property in the anonymous type a different explicit name rather than simply relying on the existing name. You can then access them later using:

dr["Description"] = lAreaType[j].LtDescription;
dr["CatDescription"] = lAreaType[j].CatDescription;
0

精彩评论

暂无评论...
验证码 换一张
取 消