按 var 值填充的通用集合

发布于 2024-12-08 20:47:39 字数 356 浏览 1 评论 0原文

在我的代码中,我需要帮助将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

荆棘i 2024-12-15 20:47:39

您的结果不包含列表中所需的整个对象。
因此您可以执行以下操作。

 NorthwindDataContext db = new NorthwindDataContext();
            List<Category> oList = new List<Category>();
            oList.AddRange(db.Categories);

如果添加到列表不是严格要求的,那么您可以简单地将结果集转换为列表,如下所示:

 NorthwindDataContext db = new NorthwindDataContext();
            List<Category> oList = db.Categories.ToList();

但是您需要知道这是示例代码。像这样拉动整个表可能不是最好的做法(除非您知道表中的记录数量是固定的,不会改变,并且将它们全部加载到内存中是安全的)。

Your result doesn't have the entire object that is required in the list.
So you can do the following instead.

 NorthwindDataContext db = new NorthwindDataContext();
            List<Category> oList = new List<Category>();
            oList.AddRange(db.Categories);

If adding to list is not strictly required then you can simply convert the result set to list like so:

 NorthwindDataContext db = new NorthwindDataContext();
            List<Category> oList = db.Categories.ToList();

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).

带上头具痛哭 2024-12-15 20:47:39

当前结果是匿名类型的集合,您希望它返回一个类别。

NorthwindDataContext db = new NorthwindDataContext();
            List<Category> oList = new List<Category>();
            var result = from p in db.Categories
                         select new Category { CategoryID = p.CategoryID, CategoryName=p.CategoryName };

然后,您可以将类别添加到 oList -

oList.AddRange(result.ToList());

编辑:

好的,鉴于您只想从数据库中获取一些字段,请创建一个仅包含这些字段的新类型(如果您不需要在你的方法之外使用它,你不必这样做,只需将其保留为匿名类型) -

class CategorySml
{
  public int CategoryID {get; set;}
  public string CategoryName {get; set;}
}

...

NorthwindDataContext db = new NorthwindDataContext();
            List<CategorySml> oList = new List<Category>();
            var result = from p in db.Categories
                         select new CategorySml { CategoryID = p.CategoryID, CategoryName=p.CategoryName };

Currently result is a collection of an anonymous type, you want it to return you a Category instead.

NorthwindDataContext db = new NorthwindDataContext();
            List<Category> oList = new List<Category>();
            var result = from p in db.Categories
                         select new Category { CategoryID = p.CategoryID, CategoryName=p.CategoryName };

You can then add your Categories to oList -

oList.AddRange(result.ToList());

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

class CategorySml
{
  public int CategoryID {get; set;}
  public string CategoryName {get; set;}
}

...

NorthwindDataContext db = new NorthwindDataContext();
            List<CategorySml> oList = new List<Category>();
            var result = from p in db.Categories
                         select new CategorySml { CategoryID = p.CategoryID, CategoryName=p.CategoryName };
挖个坑埋了你 2024-12-15 20:47:39
NorthwindDataContext db = new NorthwindDataContext();
        List<Category> oList = db.Categories.ToList();

编辑:

假设Category类是您自己的

NorthwindDataContext db = new NorthwindDataContext();
        List<Category> oList = db.Categories.Select(p => new Category { CategoryID = p.CategoryID, CategoryName=p.CategoryName }).ToList();
NorthwindDataContext db = new NorthwindDataContext();
        List<Category> oList = db.Categories.ToList();

Edit:

Assuming that Category class is your own

NorthwindDataContext db = new NorthwindDataContext();
        List<Category> oList = db.Categories.Select(p => new Category { CategoryID = p.CategoryID, CategoryName=p.CategoryName }).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文