聚合一对多的一方并绑定到Entity Framework中的gridview [英] Aggregating one side of a many-to-many and bind to gridview in Entity Framework

查看:255
本文介绍了聚合一对多的一方并绑定到Entity Framework中的gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


CompanyNotice (MM)我的数据库/实体框架模型中有一个多对多的结构) CompanyNoticesLocations (MM)位置


  if(!IsPostBack)
{
使用(var context = new ALEntities())
{
var query = from c in context.CompanyNotices.Include(Locations)
选择新
{
c.CompanyNoticeHeading,
c.CompanyNoticeText,
c.IsHR,
locations =(from c in c.Locations select l.LocationName).Aggregate((current,next)=> string .Format({0},{1},当前,下一个))

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

当我尝试上述代码时,我收到以下错误:


LINQ to Entities不会识别方法'System.String
Aggregate [String](System.Collections.Generic。 IEnumerable 1 [System.String],
System.Func
3 [System.String,System.String,System.String])'方法和
这个方法不能被翻译成一个存储表达式。


当我尝试:



$($)
{
使用(var context = new ALEntities())
{
var query = from c在上下文中.CompanyNotices.Include(Locations)
选择新
{
c.CompanyNoticeHeading,
c.CompanyNoti ceText,
c.IsHR,
locations =(从c.Locations中的l选择l.LocationName)

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

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


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


有没有人知道如何聚合一个CompanyNotice出现的LocationName?



提前感谢。

解决方案

你可以这样...

  (var context = new ALEntities())
{
var query = from c in context.CompanyNotices.Include(Locations)
选择新
{
c .CompanyNoticeHeading,
c.CompanyNoticeText,
c.IsHR,
loc ations =(from c 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)});

然后将逗号列表绑定到您的datagrid


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

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

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();
                }   
            }

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

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.

When I try:

 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();
                    }   
                }

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

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

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

Thanks in advance.

解决方案

you could this ...

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

then bind commaListed to your datagrid

这篇关于聚合一对多的一方并绑定到Entity Framework中的gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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