linq group by with projection with class with non-parameterless constructor

时间:2015-03-26 18:05:05

标签: c# asp.net linq projection

我无法通过linq语句执行组操作,该语句投影到Foo列表中,其中每个Foo也可以包含Bar列表。在我的例子中,这两个类都没有无参数构造函数,所以我在语法上有点迷失。

https://dotnetfiddle.net/9zplqa

public class linqGrouping
{

    public linqGrouping()
    {
        var data = this
            .TestData()
            .GroupBy(gb => new { gb.GroupProperty, gb.RandomProperty })
            .Select(s => new Foo(
                s.Key.GroupProperty, 
                s.Key.RandomProperty, 
                // Here's where I'm having issues, I was the non key parts of the group to be placed into a list of bar, I'm not sure how to access the "Values" as opposed to the Key
                new List<Bar>()
                {
                         // vvv Here's my problem area vvv //
                    //new Bar(someProperty, someOtherProperty)
                         // ^^^ Here's my problem area ^^^ //
                }
            ));

        // The Expected object I'm hoping to create would be:
        List<Foo> foo = new List<Foo>()
        {
            new Foo(1, 1, new List<Bar>()
                    {
                        new Bar(1, "test"),
                        new Bar(2, "test2")
                    }),
            new Foo(2, 1, new List<Bar>()
                    {
                        new Bar(1, "test")
                    })
        };
    }

    public List<RawData> TestData()
    {
        List<RawData> data = new List<RawData>();

        data.Add(new RawData()
                 {
                     GroupProperty = 1,
                     RandomProperty = 1,
                     SomeOtherProperty = "test",
                     SomeProperty = 1
                 });

        data.Add(new RawData()
                 {
                     GroupProperty = 1,
                     RandomProperty = 1,
                     SomeOtherProperty = "test2",
                     SomeProperty = 2
                 });

        data.Add(new RawData()
                 {
                     GroupProperty = 2,
                     RandomProperty = 1,
                     SomeOtherProperty = "test",
                     SomeProperty = 1
                 });

        return data;
    }

}

public class RawData
{
    public int GroupProperty { get; set; }
    public int RandomProperty { get; set; }
    public int SomeProperty { get; set; }
    public string SomeOtherProperty { get; set; }
}

public class Foo
{
    public int GroupProperty { get; private set; }
    public int RandomProperty { get; private set; }
    public List<Bar> Bars { get; private set; }

    public Foo(int groupProperty, int randomProperty, List<Bar> bars)
    {
        this.GroupProperty = groupProperty;
        this.RandomProperty = randomProperty;
        this.Bars = bars;
    }
}

public class Bar
{
    public int SomeProperty { get; private set; }
    public string SomeOtherProperty { get; private set; }

    public Bar(int someProperty, string someOtherProperty)
    {
        this.SomeProperty = someProperty;
        this.SomeOtherProperty = someOtherProperty;
    }
}

我该怎么做呢?有没有比我进入的方向更好的方法呢?

1 个答案:

答案 0 :(得分:4)

要从分组中选择另外两个属性到Bar的集合,您只需执行以下操作:

var data = this.TestData()
    .GroupBy(gb => new { gb.GroupProperty, gb.RandomProperty })
    .Select(s => new Foo(
         s.Key.GroupProperty, 
         s.Key.RandomProperty, 
         s.Select(b => new Bar(b.SomeProperty, b.someOtherProperty)).ToList())
        ));