如何实现IEnumerable以便在LinQ查询中使用自定义类? [英] How do I implement IEnumerable so that I can use a custom class in a LinQ query?
问题描述
我使用Entity Framework连接到SQL Server数据库(Northwind) (2008R2)。然后我使用LinQ查询使用Customers表中的3列数据填充Asp.Net Gridview控件。 这一切都可以在没有和使用Lambdas
的情况下工作但是........
I am connecting to a sql server Database (Northwind) (2008R2) using Entity Framework. Then I am using a LinQ Query to populate an Asp.Net Gridview control with 3 columns of data from the Customers table. This all works without and with Lambdas but ........
我在没有和使用Lambdas的情况下尝试过这个LinQ查询的几个版本。 在我的阅读中,似乎如果我在查询中使用自定义类对象,这为我提供了更多功能(我是否正确理解了该部分?)。
我在(网络表单)代码后面添加了一个(普通)类对象,其中包含我在查询中使用的3列的3个属性。 我可以在不使用Lambdas的LinQ查询中使用此类对象。 但是,当我尝试在 DOES 的查询中使用类对象
时,使用Lambda我收到以下错误消息:
I have tried a few versions of this LinQ query without and with Lambdas. In my readings it appears that if I use a custom class object in the query this provides me with more versatility (am I understanding that part correctly?). I added a (plain) class object to the (web form) code behind with 3 properties for the 3 columns I am using in the query. I can use this class object in the LinQ query which does not use the Lambdas. But when I try to use the class object in the query that DOES use the Lambda I receive the following error message:
>>无法使用集合初始值设定项初始化GridviewProject.CustomerSet,因为它没有实现System.Collections.IEnumerable<<
>>Cannot initialize type GridviewProject.CustomerSet with a collection initializer because it does not implement System.Collections.IEnumerable<<
我尝试在类上实现IEnumerable,但是然后我收到没有添加功能的错误消息。 LinQ查询如下:
I tried implementing IEnumerable on the class, but then I get the error message that there is no Add function. The LinQ queries are below:
- 没有Lambdas的LinQ查询
--LinQ query Without the Lambdas
private void GetCustomersPageWise(int pageIndex)
{
//--LinQ query without the Lamda -- I can use the CustomerSet class here
var dataset1 = (from rs in db.Customers
select new CustomerSet
{
CustomerId = rs.CustomerID,
ContactName = rs.ContactName,
CompanyName = rs.CompanyName
}).ToList();
//--LinQ query with the Lamda -- I get the error message here if I try using
//--CustomerSet class in the query -- how to implement IEnumerable?
// - 如果我不使用CustomerSet类,则此Lambda查询正常工作
var dataset2 = db.Customers
.Select(x => new {x.CustomerID, x.ContactName,x.CompanyName})。ToList();
GridView1.DataSource = dataset1;
GridView1.DataBind();
}
公共类CustomerSet
{
public string CustomerId {get;组; }
公共字符串ContactName {get;组; }
public string CompanyName {get;组; }
}
//--this Lambda query works OK if I don't use CustomerSet class var dataset2 = db.Customers .Select(x => new { x.CustomerID, x.ContactName, x.CompanyName }).ToList(); GridView1.DataSource = dataset1; GridView1.DataBind(); } public class CustomerSet { public string CustomerId { get; set; } public string ContactName { get; set; } public string CompanyName { get; set; } }
我问我的问题主要是了解如何在这种情况下实施IEnumerable。 但我也想知道在LinQ查询中使用像CustomerSet这样的类有什么好处? 有优势吗? 这个优势是什么
以及我在项目中如何/在哪里使用这个优势?
I ask my question mainly to understand how to go about implement IEnumerable in this scenario. But I am also wondering what is the advantage of using a class like CustomerSet in the LinQ Query? Is there an advantage? What is this advantage and how/where would I use this advantage in my project?
如果重要,这里是Gridview控件的标记:
In case it matters, here is the markup for the Gridview control:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" PageSize="10" AllowPaging="true"
OnPageIndexChanging="GridView1_PageIndexChanging">
<Columns>
<asp:BoundField HeaderText="CustomerId" DataField="CustomerId" />
<asp:BoundField HeaderText="ContactName" DataField="ContactName" />
<asp:BoundField HeaderText="CompanyName" DataField="CompanyName" />
</Columns>
</asp:GridView>
任何建议赞赏
Any suggestions appreciated
Rich P
推荐答案
试试这个:
var dataset2 = db
.Customers
.Select(x => new CustomerSet { CustomerId = x.CustomerID, ContactName = x.ContactName, CompanyName = x.CompanyName })
.ToList();
您还可以编写'new CustomerSet(x)'或'new CustomerSet(x .CustomerID,x.ContactName,x.CompanyName)'如果为
CustomerSet 类定义相应的构造函数。
You can also write 'new CustomerSet( x )' or 'new CustomerSet( x.CustomerID, x.ContactName, x.CompanyName )' if you define the corresponding constructors for
CustomerSet class.
这篇关于如何实现IEnumerable以便在LinQ查询中使用自定义类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!