根據類結構更改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 联系我们: