Linq,获取按字段分组的不同元素

发布于 2024-11-29 07:22:41 字数 636 浏览 0 评论 0原文

我觉得很愚蠢,但我不得不承认,我需要你们的帮助,在看似很容易找到正确答案的情况下,

 public class Foo
 {
     public string Name { get; set; }
     public string Time { get; set; }

     public Foo(string name, string time)
     {
        Name = name;
        Time = time;
     }
 }

var o = new List<Foo>
                  {
                      new Foo("Breakfast","9:00"),
                      new Foo("Lunch", "12:00"),
                      new Foo("Breakfast", "8:00")
                  };

如何只吃 9:00 的早餐和午餐,而不包括 8 点的早餐?

我是这样开始的:

var a = o.GroupBy(x => x.Name);

但它只会给你名字,而我也需要时间。

I feel stupid, but I have to admit, I need your help guys in seemingly easy to find the right answer situation

 public class Foo
 {
     public string Name { get; set; }
     public string Time { get; set; }

     public Foo(string name, string time)
     {
        Name = name;
        Time = time;
     }
 }

var o = new List<Foo>
                  {
                      new Foo("Breakfast","9:00"),
                      new Foo("Lunch", "12:00"),
                      new Foo("Breakfast", "8:00")
                  };

How to get only Breakfast at 9:00 and Lunch, and not include Breakfast at 8?

I started like that:

var a = o.GroupBy(x => x.Name);

But it will give you only Name, where I need Time too.

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

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

发布评论

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

评论(3

从此见与不见 2024-12-06 07:22:41
var a = o.GroupBy(x => x.Name).Select(g => g.First())

但想想 .First() 调用。当存在多个元素时,您希望如何选择正确的元素 - 您可能希望对元素进行排序以获得最新/最早的元素,或者无论您的要求是什么。

var a = o.GroupBy(x => x.Name).Select(g => g.First())

But think about that .First() call. How do you want to select the correct element when there is more than one - you might want to sort the elements to get the latest / earliest, or whatever your requirement is.

三月梨花 2024-12-06 07:22:41

一种选择是:

var a = o.GroupBy(x => x.Name)
         .Select(g => g.First());

或者您可以使用 DistinctBy< /code>来自 MoreLINQ

var a = o.DistinctBy(x => x.Name);

后者的内存效率更高,并且会立即开始产生结果,而第一种方法必须读取所有数据,然后才能返回任何内容。另一方面,后者需要 MoreLINQ(或复制代码)。

编辑:这里我假设 LINQ to Objects(因为 MoreLINQ 当然不会与其他提供者一起工作),并且您对代表该组的任何组的第一次出现感到满意。

One option is:

var a = o.GroupBy(x => x.Name)
         .Select(g => g.First());

Or you could use DistinctBy from MoreLINQ:

var a = o.DistinctBy(x => x.Name);

The latter is more efficient in memory, and will start yielding results immediately, whereas the first approach has to read all the data before it can return anything. On the other hand, the latter requires MoreLINQ (or copying the code).

EDIT: Here I'm assuming LINQ to Objects (as MoreLINQ certainly won't work with other providers) and that you're happy with the first occurrence for any group being representative of that group.

一绘本一梦想 2024-12-06 07:22:41

为什么不像下面这样呢? (我还冒昧地让你的代码变得更小——我认为由于你的属性是公共的,你不需要构造函数,而可以只使用对象初始化语法。这应该全部在 LinqPad 中运行:

void Main() {
    var os = new [] {
        new Foo{Name="Breakfast",Time="9:00"},
        new Foo{Name="Lunch", Time="12:00"},
        new Foo{Name="Breakfast", Time="8:00"}
    };

    os.Where(o => DateTime.Parse(o.Time).Hour > 8).Dump();
}

public class Foo
{
    public string Name { get; set; }
    public string Time { get; set; }
}

Why not something like the following? (I also took the liberty of making your code a bit smaller -- I think since your properties are public, you don't need a constructor, but can just use object-initializer syntax instead. This should all run in LinqPad:

void Main() {
    var os = new [] {
        new Foo{Name="Breakfast",Time="9:00"},
        new Foo{Name="Lunch", Time="12:00"},
        new Foo{Name="Breakfast", Time="8:00"}
    };

    os.Where(o => DateTime.Parse(o.Time).Hour > 8).Dump();
}

public class Foo
{
    public string Name { get; set; }
    public string Time { get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文