好吧,我有以下情况:
public class Joins<TOuter, TInner, TResult>
{
public Expression<Func<TOuter, object>> outerKeySelector;
public Expression<Func<TInner, object>> innerKeySelector;
public Expression<Func<TOuter, TInner, object>> resultSelector;
public IEnumerable<TResult> r;
}
public class Test<T>
{
public IEnumerable<TResult> Join<TInner, TResult>(
Expression<Func<T, object>> outerKeySelector,
Expression<Func<TInner, object>> innerKeySelector,
Expression<Func<T, TInner, TResult>> resultSelector) where TInner : class
{
var join = new Joins<T, TInner, TResult>();
join.innerKeySelector = innerKeySelector;
join.outerKeySelector = outerKeySelector;
return join.r;
}
}
要创建加入方法,我依靠链接: http: //msdn.microsoft.com/en-us/library/bb534644 (v=vs.100).aspx
但是,当我尝试调用该方法时,无法识别TInner,使该方法变得无效,我返回以下错误:
无法将表达式类型“type”转换为返回类型“TResult”
注意:'Joins'类纯粹是一个测试,没有一个是确定的,var 'r'仅用于测试,仅用于方便返回。
预期用途示例:
var test = new Test<User>().Join<Permission>(u => u.Id, p => p.IdUser, (u, p) => new { Id = u.Id , Area = p.Area });
更多细节:
如前所述,无法识别TInner,因此我无法调用正确的方法。
作为测试,我这样做了:
var test = new Test<User>().Join<Permission>(u => u.Id, p => p.ToString(), (u, p) => new {Id = u.Id, Name = p.ToString()});
我知道p.ToString()不正确,但无法识别指示的类的属性(在 Permission 的情况下),然后放入 ToString() 只是为了完成编写方法。
编辑:
我需要在 foreach/for 中使用结果
例子:
foreach(var obj in test)
{
var id = obj.Id;
var area = obj.Area;
.
.
.
}