【发布时间】:2017-09-17 02:19:44
【问题描述】:
我已经玩了好几个小时了,并且可以使用新的眼睛。好吧,在表达树方面比我更有经验的人。曲线很陡!
考虑以下内容。 (可行,但我需要将 Accounts 作为字符串传入。Accounts 是 Account 列表)
repo = repo.Where(x => x.Accounts.FirstOrDefault().Id == AccountId);
这是我目前所拥有的。
var parameterExp = Expression.Parameter(typeof(Boat), "x");
var propertyExp = Expression.Property(parameterExp, "Accounts");
MethodInfo method1 = typeof(Queryable).GetMethods(BindingFlags.Public | BindingFlags.Static).First(m => m.Name == "FirstOrDefault");
// This gives me a Queryable of Account FirstOrDefault
var specificMethod = method1.MakeGenericMethod(propertyExp.Type);
// right here is where I am getting stuck
var firstOrDefaultAccountExpression = // I need to get an Account so I can query against the Id ??
MethodInfo method = typeof(long).GetMethod("Equals", new[] { typeof(long) });
var someValue = Expression.Constant(AccountId, typeof(long));
var containsMethodExp = Expression.Call(firstOrDefaultAccountExpression , method, someValue);
Expression<Func<Boat, bool>> predicate = Expression.Lambda<Func<Boat, bool>>
(containsMethodExp, parameterExp);
repo = repo.Where(predicate);
我能够让此代码在常规字符串成员上工作。我似乎无法弄清楚名单。最终,我试图找回 AccountId = long 的 Boat 列表
提前致谢!
【问题讨论】:
-
您只关心每艘船的第一个帐户吗?
-
是的,它实际上可能是 Singe 而不是 FirstOrDefault。船只将永远只有一个与之相关的帐户。该帐户是船主
标签: c# linq lambda tree expression