使用匿名类型声明列表

时间:2018-05-07 00:59:06

标签: c# .net list entity-framework linq

我正在尝试声明其中包含匿名类型的列表。我试图将列表值分配给null,并尝试将列表分配给新的实体列表,但它显示错误。有什么指针吗?

var finalEntries = new List<MyDbTableEntity>();

var groupedItemList = _context.MyDbTableEntity
                      .Select(k => new { name = k.name.ToString(), data = k.info.ToString() })
                      .GroupBy(k => k.name)
                      .ToList();

finalEntries.AddRange(groupedItemList);

错误

cannot convert from 
'System.Collections.Generic.List<System.Linq.IGrouping<string, <anonymous type: string name, string data>>>' 
to 
'System.Collections.Generic.IEnumerable<MyDbTableEntity>'

3 个答案:

答案 0 :(得分:4)

它不起作用,因为您有一个MyDbTableEntity的类型列表。您无法在此类型列表中添加其他类型。尝试定义正确的类型?

var finalEntries = new List<MyDbTableEntity>();

var groupedItemList = _context.MyDbTableEntity
                      .Select(k => new MyDbTableEntity { Name = k.name.ToString(), Data = k.info.ToString() })
                      .GroupBy(k => k.Name)
                      .ToList();

finalEntries.AddRange(groupedItemList);

或者将其更改为object列表。

var finalEntries = new List<object>();

// not sure about your type
var groupedItemList = _context.MyDbTableEntity
                      .Select(k => new { name = k.name.ToString(), data = k.info.ToString() })
                      .GroupBy(k => k.name)
                      .ToList();

finalEntries.AddRange(groupedItemList);

Importante :问题是当你丢失这个范围时你不会得到一个类型化的匿名类型,我的意思是,你需要使用一些工件来读取属性,对于示例:使用reflection将类型转换为dictionary

答案 1 :(得分:1)

在此示例中,Select().GroupBy().ToList()基本上返回列表列表。顶级列表包含GroupBy()创建的所有组,而第二级列表包含从Select()方法返回的每个组的行。这些是匿名类型的对象,与MyDbTableEntity完全不同

组列表和匿名类型对象都不能转换为MyDbTableEntity类型。为了证明我的意思,如果你删除了Select()语句(以及匿名类型),你可以将最后一行更改为以下内容,它可以工作:

foreach(var items in groupedItemList) finalEntries.AddRange(items);

当然,这首先打破了分组的目的,但它确实证明了原始代码无法正常工作的一个方面。第二个方面是通过删除Select()语句

来演示的

这是我的意思的一个例子:

    class Person {
        public string FirstName;
        public string LastName;
        public Person(string firstName, string lastName) {
            FirstName = firstName;
            LastName = lastName;
        }
    }

    Person[] people = {
        new Person("Fred", "Bloggs"),
        new Person("Linda", "Bloggs"),
        new Person("Joe", "Bloe"),
        new Person("Jane", "Bloe"),
        new Person("Fred", "Flinstone")};

    public void Test() {
        List<Person> list = new List<Person>();     

        // This works   
        var peopleGroupedByLastName = people.GroupBy(k => k.LastName).ToList();
        foreach(var grp in peopleGroupedByLastName) {
            // The grp var holds a list of Person objects, each with the same last name
            list.AddRange(grp); // Pointless, but works
        }

        // The following will not even compile because the anonymous typed objects are
        // not compatible with Person objects, despite having exactly the same fields
        var peopleGroupedByLastName2 = people
            .Select(k => new { FirstName = k.FirstName, LastName = k.LastName })
            .GroupBy(k => k.LastName)
            .ToList();
        foreach(var grp in peopleGroupedByLastName2)
            list.AddRange(grp); // Not "Person" objects
    }

答案 2 :(得分:1)

var finalEntries = Enumerable.Empty<MyDbTableEntity>()
                      .Select(k => new MyDbTableEntity { Name = k.name.ToString(), Data = k.info.ToString() })
                      .GroupBy(k => k.Name)
                      .ToList();

var groupedItemList = _context.MyDbTableEntity
                      .Select(k => new MyDbTableEntity { Name = k.name.ToString(), Data = k.info.ToString() })
                      .GroupBy(k => k.Name)
                      .ToList();

finalEntries.AddRange(groupedItemList);

http://rextester.com/KNLMR37736