如何从 Linq 查询中提取结果?

发布于 2024-09-16 09:24:31 字数 612 浏览 8 评论 0原文

class Program
{
    static void Main(string[] args)
    {
        MyDatabaseEntities entities = new MyDatabaseEntities();

        var result = from c in entities.Categories
                        join p in entities.Products on c.ID equals p.IDCategory
                        group p by c.Name into g
                        select new
                        {
                            Name = g.Key,
                            Count = g.Count()
                        };

        Console.WriteLine(result.ToString());
        Console.ReadLine();
    }
}

如何从结果集中提取值以便使用它们?

class Program
{
    static void Main(string[] args)
    {
        MyDatabaseEntities entities = new MyDatabaseEntities();

        var result = from c in entities.Categories
                        join p in entities.Products on c.ID equals p.IDCategory
                        group p by c.Name into g
                        select new
                        {
                            Name = g.Key,
                            Count = g.Count()
                        };

        Console.WriteLine(result.ToString());
        Console.ReadLine();
    }
}

How can I extract the values from ths result set so I can work with them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

甜味拾荒者 2024-09-23 09:24:31
foreach (var item in result)
{
    var name = item.Name;
    var count = item.Count;
    ...
}

这只适用于 LINQ 查询所在的同一方法,因为编译器只会知道 LINQ 中使用的匿名对象类型 (new { }) 中哪些属性可用。选择

如果将 LINQ 查询返回到调用方法,并且希望以上面所示的方式访问它,则必须定义显式类型并在 LINQ 查询中使用它:

class NameCountType
{
    public string Name { get; set; }
    public int Count { get; set; }
}

...

return from ... in ...
       ...
       select new NameCountType
              {
                  Name = ...,
                  Count = ...,
              };
foreach (var item in result)
{
    var name = item.Name;
    var count = item.Count;
    ...
}

This will only work inside the same method where the LINQ query is located, since the compiler will only then know which properties are available in the anonymous object type (new { }) used in your LINQ select.

If you return a LINQ query to a calling method, and you want to access it in the way shown above, you'd have to define an explicit type and use it in your LINQ query:

class NameCountType
{
    public string Name { get; set; }
    public int Count { get; set; }
}

...

return from ... in ...
       ...
       select new NameCountType
              {
                  Name = ...,
                  Count = ...,
              };
落墨 2024-09-23 09:24:31

例如:

foreach (var x in result)
{
   Console.WriteLine(x.c.Name);
}

For example:

foreach (var x in result)
{
   Console.WriteLine(x.c.Name);
}
书信已泛黄 2024-09-23 09:24:31
 var results = (from myRow in ds.Tables[0].AsEnumerable()
                  where myRow.Field<String>("UserName") == "XXX"
                  select myRow).Distinct();
 foreach (DataRow dr in results)
    UserList += " , "+dr[0].ToString();
 var results = (from myRow in ds.Tables[0].AsEnumerable()
                  where myRow.Field<String>("UserName") == "XXX"
                  select myRow).Distinct();
 foreach (DataRow dr in results)
    UserList += " , "+dr[0].ToString();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文