【问题标题】:How to differentiate between two similar fields in Linq Join tables如何区分 Linq 连接表中的两个相似字段
【发布时间】:2011-03-03 12:39:49
【问题描述】:

如何区分两个选择的新字段,例如说明

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)
    {
    }

【问题讨论】:

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


    【解决方案1】:

    你可以在选择时给他们一个明确的名字:

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

    然后:

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

    【讨论】:

      【解决方案2】:

      变化:

      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;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-11-21
        • 1970-01-01
        • 2021-07-13
        • 2021-10-22
        • 1970-01-01
        • 1970-01-01
        • 2022-08-18
        • 2011-10-15
        相关资源
        最近更新 更多