如何区分Linq Join表中的两个相似字段

时间:2010-06-14 13:28:43

标签: asp.net linq linq-to-sql .net-3.5 linq-to-objects

如何区分两个选择的新字段,例如描述

c.Description和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++)
        {
            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)
    {
    }

2 个答案:

答案 0 :(得分:2)

选择时,您可以给他们一个明确的名称:

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

然后:

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

答案 1 :(得分:0)

变化:

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

要:

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

这将为匿名类型中的每个属性提供不同的显式名称,而不是简单地依赖于现有名称。然后,您可以使用以下方式访问它们:

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