根据类结构更改GroupBy键

[英]Changing GroupBy keys depending on Class Structure


I have a list of items with multiple columns and would like to group them by some fields depending on a boolean:

我有一个包含多列的项目列表,并希望根据布尔值将它们分组为某些字段:

I have the following class:

我有以下课程:

public class Item
    {
        public string Group { get; set; }
        public string Person { get; set; }
        public string Currency { get; set; }
        public string Country { get; set; }
        public string County { get; set; }
        public string OtherAdd { get; set; }
        public string Income { get; set; }
    }

which is part of a List:

这是List的一部分:

var results = items.ToList(); //items is IEnumerable<Item>

if int type = 1, then I want to group by more elements:

如果int type = 1,那么我想按更多元素分组:

results = results
                .GroupBy(e => new { e.Group, e.Person, e.Branch, e.Currency, e.Country, e.County, e.OtherAdd})
                .Select(g => new Item
                {
                    Group = g.Key.Group,
                    Person = g.Key.Person,
                    Currency = g.Key.Currency,
                    Currency = g.Key.Country,
                    Currency = g.Key.County,
                    Currency = g.Key.OtherAdd,
                    Income = g.Sum(p => double.Parse(p.Income, System.Globalization.CultureInfo.InvariantCulture)).ToString("0.00", System.Globalization.CultureInfo.InvariantCulture)
                })
                .ToList();

if int type = 2, then I want to group by fewer elements (e.g. because OtherAdd would be an empty String):

如果int type = 2,那么我想用更少的元素进行分组(例如,因为OtherAdd将是一个空字符串):

results = results
                .GroupBy(e => new { e.Group, e.Person, e.Branch, e.Currency})
                .Select(g => new Item
                {
                    Group = g.Key.Group,
                    Person = g.Key.Person,
                    Currency = g.Key.Currency,
                    Income = g.Sum(p => double.Parse(p.Income, System.Globalization.CultureInfo.InvariantCulture)).ToString("0.00", System.Globalization.CultureInfo.InvariantCulture)
                })
                .ToList();

etc.

Is there a way for me to change the GroupBy key depending on my integer type without repeating the code?

有没有办法让我根据我的整数类型更改GroupBy键而不重复代码?

1 个解决方案

#1


Well, you could use the old SQL trick, conditional values:

好吧,你可以使用旧的SQL技巧,条件值:

.GroupBy(e => new { e.Group, Person = (e.Type == 1 ? e.Person : Guid.NewGuid().ToString()), ... }

While this will still include the columns in the group by, all the items will have unique keys, so it doesn't quite matter. Sadly, I don't think there's a way around generating the unique keys, unlike in SQL (where you could just use NULL).

虽然这仍然包括group by中的列,但所有项都将具有唯一键,因此它并不重要。遗憾的是,我不认为有一种方法可以生成唯一键,这与SQL(你只能使用NULL)不同。

A better way might be to implement your own grouping class, instead of using an anonymous type. You could then use your own equality and hashing semantics, to make sure whether you include all the fields or not. However, that is arguably going to be more work than just having the similar code repeated.

更好的方法可能是实现自己的分组类,而不是使用匿名类型。然后,您可以使用自己的相等和散列语义,以确定是否包含所有字段。然而,这可能比仅重复使用相似的代码更有用。

Or, you might want to revise your whole design. It doesn't sound like what you're trying to do makes much sense - it's already quite suspicious that you're using the same type for two different things, and using strings for all the fields doesn't help either. Maybe you could try a different object design?

或者,您可能想要修改整个设计。这听起来并不像你想要做的那样有意义 - 你已经非常怀疑你为两个不同的东西使用相同的类型,并且对所有字段使用字符串也无济于事。也许你可以尝试不同的对象设计?


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/05/12/2f61908de4f625c2f0fa7a3aab810e48.html



 
  © 2014-2022 ITdaan.com 联系我们: