我的問題由下面的代碼示例說明。 我如何在其他地方放置部分lambda?從lambda表達式到多重LINQ表達式的拆分邏輯
如何從我的'GetGurus()'方法調用'Foo'? 我希望LINQ將它翻譯爲1條語句。
public enum GuruLevel
{
NotSet,
Goku,
SuperSayan
}
private IEnumerable<PersonInfo> GetGurus()
{
using (var context = new CRMContext())
{
var persons = context.Person
.Where(p => p.Experience > 10)
.OrderBy(p => p.DateOfBirth)
.Select(p => new PersonInfo())
{
StackOverFlowName = p.StackOverFlowName,
Experience = p.Experience,
GuruStatus = Foo //(p.Experience > 9000) ? GuruLevel.SuperSayan : GuruLevel.Goku
}
return persons.ToList();
}
}
public static System.Linq.Expressions.Expression<Func<Person, GuruLevel>> Foo
{
get
{
return bar => (bar.Experience > 9000) ? GuruLevel.SuperSayan : GuruLevel.Goku;
}
}
http://stackoverflow.com/questions/27324641/reusable-calculations-for-linq-projections-in-entity-framework-code-first – jeroenh
我讀你的鏈接,看來這正是我需要。雖然看起來相當複雜: –
太糟糕了,您沒有發佈答案,因爲您給出了最好的答案!我安裝了[DelegateDecompiler](https://www.nuget.org/packages/DelegateDecompiler/)。 –