无法解析财产(linq / lambda)

时间:2012-01-26 11:14:47

标签: .net lambda linq-to-entities

我正在尝试使用LINQ / LAMBDA访问属性

List<Latest> latestBooks = DataContext.Session.Query<Book>().Where(x=> x.Enabled == "True" ).Select(x => new Latest(x.Title,x.Author)).Take(10).ToList();

但Book类中定义的另一个属性如下所示:

public virtual string FrontEndLink { get { return string.Format("http://myurl/{0}", Filename); } }

当我尝试这个时

List<Latest> latestBooks = DataContext.Session.Query<Book>().Where(x=> x.Enabled == "True" ).Select(x => new Latest(x.FrontEndLink)).Take(10).ToList();

代码中断并给我一个错误:

Could not resolve property: Quote((x, ) => (x.Id)), ), Quote((x, ) => (new Latest(x.FrontEndLink, ))), ), p1, )]

1 个答案:

答案 0 :(得分:3)

LINQ to Entities正试图将ToList之前的所有内容转换为SQL - 并且无法使用FrontEndLink属性执行此操作。您希望在LINQ to Objects中执行该操作,但过滤后仅执行10个结果。所以我会用:

var latestBooks = DataContext.Session.Query<Book>()
                             // Do the first bit in the database
                             .Where(x=> x.Enabled == "True")
                             .Take(10)
                             // Do the rest in LINQ to Objects
                             .AsEnumerable()
                             .Select(x => new Latest(x.FrontEndLink))
                             .ToList();