LINQ联合不同类型 - 动态转换到接口? [英] LINQ Union different types - dynamically casting to an interface?

查看:205
本文介绍了LINQ联合不同类型 - 动态转换到接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有10个表作为不同类型呈现给LINQ,但共享完全相同的属性。当我尝试对它们运行联合时,编译器告诉我:

I have 10 tables which are presenting to LINQ as different types, but share exactly the same properties. When I try to run a union on them, the compiler tells me that:

"Argument 2: cannot convert from 'System.Collections.IEnumerable' to 'System.Collections.Generic.IEnumerable<LINQPad.User.TelJun2011>'"

代码如下

var jul = (from n in TelJul2011s select n);

var jun = (from p in TelJun2011s select p);

jun.Union(jul).Dump();

我做了我的研究,并明白工会不能跨不同类型执行,我也明白可以对匿名类型执行联合,如果它们共享相同的属性。这个选项不适用于我,因为我需要所有表的所有属性,不想输入相同的匿名类型10次 - 每个变量一次。我想编译器推断他们都是相同的类型基于事实,所有的属性是相同的。

I've done my research and understand that unions cannot be performed across different types, I also understand that unions can be performed on anonymous types if they share the same properties. This option will not work for me, as I need all the properties from all the tables and don't want to have to type out the same anonymous type 10 times - once for each variable. I want the compiler to infer that they are all the same type based on the fact that all properties are identical.

我已经尝试转换为IEnumberable,Iqueryable, Datatable等,同时使用AsQueryable()类型函数和as关键字。没有一个似乎为我做的伎俩。

I've already tried casting to an IEnumberable, Iqueryable, Datatable etc. using both the AsQueryable() type functions, and the "as" keyword. None of that seems to do the trick for me.

我想知道是否有一些方法这样做通过动态转换为父类型。我不能编辑类的初始声明,所以不能实现一个通用的接口来转换。但是有没有一些方法,我可以把类型转换成一个通用的接口,当他们使用,而不是从每个类型转换为父接口?

I'm wondering if there is some way of doing this by casting dynamically to a parent type. I can't edit the initial declarations of the classes, so can't implement a common interface on them to cast to. But is there some way I can downcast the types into a common interface when they are used, without writing a conversion from each type to a parent interface?

感谢任何建议!

推荐答案

Union 的结果将是 IEnumerable< Xxx> 但在这种情况下,你必须指定 Xxx 是。如果类型 TelJun2011 TelJul2011 不是 structs(值类型)利用 IEnumerable< out T> 协方差,如下所示:

The result of the Union will be an IEnumerable<Xxx> but in this case you have to specify what Xxx is. If the types TelJun2011 and TelJul2011 are not structs (value-types), you can utilize the covariance of IEnumerable<out T> like this:

jun.Union<object>(jul)

$ c> TelJun2011 和 TelJul2011 都是 object ,然后是协方差 IEnumerable< TelJun2011> IEnumerable< TelJul2011> 都是 IEnumerable< object>

This works because TelJun2011 and TelJul2011 are both object, and then by covariance IEnumerable<TelJun2011> and IEnumerable<TelJul2011> are both IEnumerable<object>.

当然 object 不具备 TelJun2011 code>和 TelJul2011 。如果这两种类型有一个更有用的共同基类或实现一个共同的接口会更好,因为那样你可以说如:

Of course object does not possess all the properties common to TelJun2011 and TelJul2011. It would be better if these two types had a more useful common base class or implemened a common interface, because then you could say e.g.:

jun.Union<ITelMonth>(jul)

其中 code>是包含所需常用属性的类型。

where ITelMonth were some type containing the common properties you want.

这篇关于LINQ联合不同类型 - 动态转换到接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆