需要显式转换通匿名类型的联盟() [英] Need to cast explicitly thru anonymous type in Union()

查看:167
本文介绍了需要显式转换通匿名类型的联盟()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个无功​​/对象,检索通这2个功能:

 私人的IQueryable<项目> SelectAll_1(...)
{
    返回查询;
}
 

类项目是:

 私人诠释身份证;
私人字符串COL1;
私人字符串COL2;
私人字符串COL3;
 

和另:

 私人的IQueryable< Project_test> SelectAll_2(...)
{
    返回查询;
}
 

其中,POCO是:

 私人字符串ID_inString;
私人字符串COL1;
私人字符串COL2;
私人字符串COL3;
 

和我需要对他们两个进行结合,

  VAR P2 = SelectAll_2(...);
VAR P1 = SelectAll_1(...);

变种P3 = P2.Union(P1);
 

但我得到一个错误,提的:

  

有关方法的类型参数System.Linq.Queryable.Union(System.Linq.IQueryable,System.Linq.Ex pressions.Ex pression>)不能从使用推断。请尝试显式指定类型参数。

我看到一些人解决通匿名类型,但我不知道它是如何工作的。任何人有任何想法?

解决方案

您不能联合两种不同的类型,除非继承自其它(例如,您的可以的,有可能找到的工会的IEnumerable<对象> 的IEnumerable<字符串> ,尽管这几乎是有益的)

现在你的情况,这听起来像项目 Project_test 确实应该设计一种类型,如果各种属性具有相同的含义。目前,该ID属性有不同的类型 - 但真的有必要或适宜?如果他们都标识有相同的范围,这是有道理的把它们存放在同一个重新presentation。如果属性的的具有相同的含义,你不应该在它们之间形成一个联盟都没有。如果他们的的具有相同的含义,你应该尽量让两个序列使用相同类型的。

您的可以的使用匿名类型对于这一点,以这种方式:

  VAR projectedP1 = P1.Select(X =>新建{x.ID,x.col1,x.col2,x.col3});
VAR projectedP2 = P2.Select(X =>新建{ID = int.Parse(x.ID_inString)
                                            x.col1,x.col2,x.col3});
VAR工会= projectedP1.Union(projectedP2);
 

或者你可以只使用现有的类型之一:

  VAR projectedP1 = P1.Select(X =>新建Project_test {
                                     ID_inString = x.ID.ToString(),
                                     COL1 = x.col1,
                                     COL2 = x.col2,
                                     COL3 = x.col3});
VAR工会= projectedP1.Union(P2);
 

这不是真的很明显这都是一个更好的主意 - 但我会回去的尝试的调和这两种类型,如果可能的话,此时您有任何问题,反正

I have 2 var/objects, retrieving thru these 2 functions:

private IQueryable<Project> SelectAll_1(...)
{
    return query;
}

class Project is:

private int ID;
private string col1;
private string col2;
private string col3;

and another:

private IQueryable<Project_test> SelectAll_2(...)
{
    return query;
}

where POCO of is:

private string ID_inString;
private string col1;
private string col2;
private string col3;

and i need to perform union on both of them,

var P2 = SelectAll_2(...);
var P1 = SelectAll_1(...);

var P3 = P2.Union(P1);

but i get an error, mentioning:

The type arguments for method 'System.Linq.Queryable.Union(System.Linq.IQueryable, System.Linq.Expressions.Expression>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

i saw some people solve it thru' anonymous type, but i not sure how it works. Anyone has any idea?

解决方案

You can't union two different types, unless one inherits from the other (for example, you could potentially find a union of IEnumerable<object> and IEnumerable<string>, although that would rarely be useful).

Now in your case, it sounds like Project and Project_test should really be one type, if the various properties have the same meaning. Currently the ID properties have different types - but is that really necessary or desirable? If they're both identifiers with the same scope, it makes sense to store them in the same representation. If the properties don't have the same meaning, you shouldn't be forming a union between them at all. If they do have the same meaning, you should try to make both sequences use the same type.

You could use anonymous types for this, in this way:

var projectedP1 = P1.Select(x => new { x.ID, x.col1, x.col2, x.col3 });
var projectedP2 = P2.Select(x => new { ID = int.Parse(x.ID_inString),
                                            x.col1, x.col2, x.col3 });
var union = projectedP1.Union(projectedP2);

Or you could just use one of the existing types:

var projectedP1 = P1.Select(x => new Project_test { 
                                     ID_inString = x.ID.ToString(), 
                                     col1 = x.col1, 
                                     col2 = x.col2, 
                                     col3 = x.col3 });
var union = projectedP1.Union(P2);

It's not really obvious which of these is a better idea - but I'd go back to trying to reconcile the two types if possible, at which point you have no problems anyway.

这篇关于需要显式转换通匿名类型的联盟()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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