var dt = from o in db.Orders
group o by o.InvoiceNo into ord
select new { OrderId = ord.OrderId, ProductName = ord.ProductName, InvocieNo = ord.InvocieNo, Price = ord.Price };
myGrid.DataSource = dt.ToList();
myGrid.DataBind();
DataBinding: 'System.Data.Objects.ELinq.InitializerMetadata+Grouping`2[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[DomainClasses.Order, DomainClasses, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'
does not contain a property with the name 'OrderId
I used db.Orders.ToList().GroupBy(x => x.InvoiceNo); and db.Orders.GroupBy(x => x.InvoiceNo).ToList(); but I get error.
Hi,
In LINQ, Group by is different from that of SQL. So if you wanna do group by, you can just use SQL's group by and import this into EntityFramework having a try.
rrasheli
Member
18 Points
31 Posts
How to group by in entity framework?
Dec 14, 2012 08:28 AM|LINK
Hi
I have this Columns in my database table.
OrderId ProductName InvoiceNo Price
1 mouse 15425 12 $
2 keyboard 14524 14 $
3 modem 15425 34 $
In this table two record have same InvoiceNo. I want to group by InvoiceNo by entity framwork and show that data in gridview asp.net.
My code is here:
private void BindGridView() { using (var db = new Context()) { var dt = db.Orders.ToList(); myGrid.DataSource = db.Orders.ToList(); myGrid.DataBind(); } }I used db.Orders.ToList().GroupBy(x => x.InvoiceNo); and db.Orders.GroupBy(x => x.InvoiceNo).ToList(); but I get error.
Please help me!
Thanks
gregaDro
Member
318 Points
65 Posts
Re: How to group by in entity framework?
Dec 14, 2012 09:41 AM|LINK
var dt = from o in db.Orders group o by o.InvoiceNo into ord select new { OrderId = ord.OrderId, ProductName = ord.ProductName, InvocieNo = ord.InvocieNo, Price = ord.Price }; myGrid.DataSource = dt.ToList(); myGrid.DataBind();rrasheli
Member
18 Points
31 Posts
Re: How to group by in entity framework?
Dec 14, 2012 11:23 AM|LINK
But in this line has error and unknow ProductName , InvoiceNo , Price.
select new { OrderId = ord.OrderId, ProductName = ord.ProductName, InvocieNo = ord.InvocieNo, Price = ord.Price };RameshRajend...
Star
7983 Points
2099 Posts
Re: How to group by in entity framework?
Dec 14, 2012 11:36 AM|LINK
Hai
Try this
rrasheli
Member
18 Points
31 Posts
Re: How to group by in entity framework?
Dec 14, 2012 11:52 AM|LINK
After bind gridview to 'dt' I get this error
DataBinding: 'System.Data.Objects.ELinq.InitializerMetadata+Grouping`2[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[DomainClasses.Order, DomainClasses, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' does not contain a property with the name 'OrderId
OrderId is myGrid DataKeyNames.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: How to group by in entity framework?
Dec 15, 2012 12:47 AM|LINK
Hi,
In LINQ, Group by is different from that of SQL. So if you wanna do group by, you can just use SQL's group by and import this into EntityFramework having a try.