按 var 值填充的通用集合
在我的代码中,我需要帮助将 result
值放入 oList
NorthwindDataContext db = new NorthwindDataContext();
List<Category> oList = new List<Category>();
var result = from p in db.Categories
select new { CategoryID = p.CategoryID, CategoryName=p.CategoryName };
我希望 oList
由结果值填充。
In my code, I need help to put result
value into oList
NorthwindDataContext db = new NorthwindDataContext();
List<Category> oList = new List<Category>();
var result = from p in db.Categories
select new { CategoryID = p.CategoryID, CategoryName=p.CategoryName };
I want oList
filled by result values.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的结果不包含列表中所需的整个对象。
因此您可以执行以下操作。
如果添加到列表不是严格要求的,那么您可以简单地将结果集转换为列表,如下所示:
但是您需要知道这是示例代码。像这样拉动整个表可能不是最好的做法(除非您知道表中的记录数量是固定的,不会改变,并且将它们全部加载到内存中是安全的)。
Your result doesn't have the entire object that is required in the list.
So you can do the following instead.
If adding to list is not strictly required then you can simply convert the result set to list like so:
You however need to know that this is sample code. Pulling the entire table like this is not probably the best thing to do (unless you know there will be fixed no. of records in table that won't change and its safe to load them all in memory).
当前结果是匿名类型的集合,您希望它返回一个类别。
然后,您可以将类别添加到 oList -
编辑:
好的,鉴于您只想从数据库中获取一些字段,请创建一个仅包含这些字段的新类型(如果您不需要在你的方法之外使用它,你不必这样做,只需将其保留为匿名类型) -
...
Currently result is a collection of an anonymous type, you want it to return you a Category instead.
You can then add your Categories to oList -
EDIT:
Ok, given that you only want to get a few fields from the database, create a new type with only those fields (if you don't need to use it outside your method, you won't have to do this, just leave it as an anonymous type) -
...
编辑:
假设
Category
类是您自己的Edit:
Assuming that
Category
class is your own