【问题标题】:Aggregating one side of a many-to-many and bind to gridview in Entity Framework聚合多对多的一侧并绑定到实体框架中的gridview
【发布时间】:2011-11-22 14:07:10
【问题描述】:

我的数据库/实体框架模型中有一个多对多结构。

CompanyNotice (M-M) CompanyNoticesLocations (M-M) 地点

我正在尝试聚合一个 CompanyNotice 的 Locations 并为 Locations 返回一个逗号分隔的 LocationName。我尝试使用以下代码来聚合 LocationName:

if (!IsPostBack)
            {
                using (var context = new ALEntities())
                {
                    var query = from c in context.CompanyNotices.Include("Locations")
                                select new 
                                { 
                                    c.CompanyNoticeHeading, 
                                    c.CompanyNoticeText,
                                    c.IsHR,
                                    locations = (from l in c.Locations select l.LocationName).Aggregate((current, next) => string.Format("{0}, {1}", current, next))

                                };
                    ASPxGridView1.DataSource = query;
                    ASPxGridView1.DataBind();
                }   
            }

尝试上述代码时出现以下错误:

LINQ to Entities 无法识别方法 'System.String 聚合 [String](System.Collections.Generic.IEnumerable1[System.String], System.Func3[System.String,System.String,System.String])' 方法,以及 此方法无法转换为商店表达式。

当我尝试时:

 if (!IsPostBack)
                {
                    using (var context = new ALEntities())
                    {
                        var query = from c in context.CompanyNotices.Include("Locations")
                                    select new 
                                    { 
                                        c.CompanyNoticeHeading, 
                                        c.CompanyNoticeText,
                                        c.IsHR,
                                        locations = (from l in c.Locations select l.LocationName)

                                    };
                        ASPxGridView1.DataSource = query;
                        ASPxGridView1.DataBind();
                    }   
                }

gridview 上的位置列中的数据显示为:

System.Collections.Generic.List`1[System.String]

有谁知道如何汇总 LocationName 以显示在一个 CompanyNotice 中?

提前致谢。

【问题讨论】:

    标签: asp.net entity-framework gridview datagridview aspxgridview


    【解决方案1】:

    你可以这样...

    using (var context = new ALEntities()) 
                        { 
                            var query = from c in context.CompanyNotices.Include("Locations") 
                                        select new  
                                        {  
                                            c.CompanyNoticeHeading,  
                                            c.CompanyNoticeText, 
                                            c.IsHR, 
                                            locations = (from l in c.Locations select l.LocationName) 
    
                                        }; 
    var listed = query.ToList();
    var commaListed = listed.Select ( a=> new { a.CompanyNoticeHeading, a.CompanyNoticeText,
    commaLines =  a.locations.Aggregate((s1, s2) => s1 + "," + s2)});
    

    然后将 commaListed 绑定到您的数据网格

    【讨论】:

    • 谢谢,这确实对我有用,但是我决定使用存储过程而不是 LINQ。
    猜你喜欢
    • 2011-02-17
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-06
    相关资源
    最近更新 更多