聚集的一侧,不少一对多并绑定到gridview的实体框架 [英] Aggregating one side of a many-to-many and bind to gridview in Entity Framework

查看:158
本文介绍了聚集的一侧,不少一对多并绑定到gridview的实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经制定了许多一对多结构,在我的数据库/实体框架模型。

I have a many-to-many structure in place in my database / Entity Framework model.

CompanyNotice (M-M) CompanyNoticesLocations (M-M)位置

CompanyNotice (M-M) CompanyNoticesLocations (M-M) Locations

我试图聚集的位置对于一个CompanyNotice并返回一个逗号分隔LOCATIONNAME的位置。我已经使用下面的code来聚合LOCATIONNAME尝试:

I am trying to aggregate the Locations for one CompanyNotice and return a comma-separated LocationName for the Locations. I have tried using the following code to aggregate the 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();
                }   
            }

我收到以下错误,当我尝试上面code:

I get the following error when I try the above code:

LINQ到实体无​​法识别方法'System.String
  总结[字符串](System.Collections.Generic.IEnumerable 1 [System.String]
  System.Func
3 [System.String,System.String,System.String])的方法,并
  这种方法不能被翻译成店前pression。

LINQ to Entities does not recognize the method 'System.String Aggregate[String](System.Collections.Generic.IEnumerable1[System.String], System.Func3[System.String,System.String,System.String])' method, and this method cannot be translated into a store expression.

当我尝试:

 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上的位置列中的数据显示为:

The data within the locations column on the gridview appears as:

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

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

有谁知道我怎么聚集LOCATIONNAME出现一个CompanyNotice?

Does anyone know how do I aggregate the LocationName to appear for one CompanyNotice?

先谢谢了。

推荐答案

您会这样...

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您的DataGrid

then bind commaListed to your datagrid

这篇关于聚集的一侧,不少一对多并绑定到gridview的实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆