简体   繁体   English

从匿名类型创建对象

[英]Create Object from Anonymous Type

I'm using EntityFramework to a list of objects from the database and I'm using Anonymous Types to eventually return the right object.我将 EntityFramework 用于数据库中的对象列表,并使用匿名类型最终返回正确的对象。 Because there are several functions that has to do this 'Anonymous type conversion' I want to extract this to functions.因为有几个函数必须执行此“匿名类型转换”,所以我想将其提取为函数。

I can create a function to create a dynamic but can't create a function that converts a dynamic in a specific type because if the function has a parameter that contains a dynamic, the return type is a dynamic type too.我可以创建一个函数来创建动态,但不能创建一个将动态转换为特定类型的函数,因为如果该函数的参数包含动态,则返回类型也是动态类型。

This works:这有效:

  List<SomeObject> list = list
            .Select(i => GetAnonymousType(i))
            .Select(i => new SomeObject {Item1 = i.Item1, Item2 =i.Item2}).ToList();

This doesn't:这不会:

List<SomeObject> list = list
         .Select(i => GetAnonymousType(i))
         .Select(i => CreateSomeObjectFromDynamic(i)).ToList();

 private static SomeObject CreateSomeObjectFromDynamic(dynamic i)
 {
     return new SomeObject {Item1 = i.Item1, Item2 = i.Item2};
 }

See: https://dotnetfiddle.net/zLFlur请参阅: https : //dotnetfiddle.net/zLFlur

Is there a way I can use a function like: CreateSomeObjectFromDynamic to return the right type?有没有办法可以使用像 CreateSomeObjectFromDynamic 这样的函数来返回正确的类型?

According to provided code at fiddle , your problem is not with CreateSomeObjectFromDynamic method, your problem is with GetAnonymousType method.根据fiddle提供的代码,您的问题不CreateSomeObjectFromDynamic方法,而在于GetAnonymousType方法。 This method returns dynamic and .NET cannot handle it.此方法返回dynamic ,.NET 无法处理它。 The compliler error says that:编译器错误说:

Cannot implicitly convert type 'System.Collections.Generic.List< dynamic >' to 'System.Collections.Generic.List< SomeObject >'无法将类型“System.Collections.Generic.List< dynamic >”隐式转换为“System.Collections.Generic.List< SomeObject >”

Even changing query to甚至将查询更改为

list = list
      .Select(i => CreateSomeObjectFromDynamic(GetAnonymousType(i)))
      .ToList();

will produce the same error.会产生同样的错误。 But, if you change return type of GetAnonymousType method to object as below:但是,如果您将GetAnonymousType方法的返回类型更改为object ,如下所示:

private static object GetAnonymousType(SomeObject i)
{
    return new { Item1 = i.Item1, Item2 = i.Item2 };
}

your problem will be solved.你的问题将得到解决。 But, this works in memory, I am not sure that it will successfully be translated into SQL if you try to use it with IQueryable for example.但是,这在内存中有效,如果您尝试将它与IQueryable一起使用,我不确定它是否会成功转换为 SQL。 Also, I do not recomend using dynamic , even though it works after chaning return type to object your code does not seem right.此外,我不建议使用dynamic ,即使它在将返回类型更改为object后仍然有效,您的代码似乎不正确。 If you make a bit effort, I am sure that you will find another way, for example using inheritance , generics and etc.如果你付出一点努力,我相信你会找到另一种方法,例如使用继承泛型等。

try this:尝试这个:

      list = list
     .Select(i => GetAnonymousType(i))
     .Select(i => CreateSomeObjectFromDynamic(i) as SomeObject).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM