代码之家  ›  专栏  ›  技术社区  ›  Azhar

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

  •  0
  • Azhar  · 技术社区  · 15 年前

    c、 说明和lt.说明

        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 回复  |  直到 15 年前
        1
  •  2
  •   Fyodor Soikin    15 年前

    您可以在选择时为它们指定一个显式名称:

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

            dr["Description"] = lAreaType[j].LtDescr;
            dr["CatDescription"] = lAreaType[j].CDescr;
    
        2
  •  0
  •   Justin Niessner    15 年前

    更改:

    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;