linq to entities:整形子查询错误

时间:2013-01-14 01:53:32

标签: c# entity-framework entity-framework-5

我试图将结果塑造成" FullList"这使用VS2012& EF5:

public class FullList {
    public int ListID {get; set;}
    public string ListName {get; set;}
    public List<ListItems> ListItems {get; set;}
}

public class ListItems {
    public int ItemID {get; set;}
    public string ItemName {get; set;}
}

这是我的查询产生恼人的错误&#34; LINQ to Entities无法识别方法... blahblahblah .....并且此方法无法转换为商店表达式。&#34 ;

List<FullList> x = (from l in entities.Lists
                   select new FullList
                   {
                       ListID = l.ListID,
                       ListName = l.ListName,
                       ListItems = l.ListItems.Select(li => new ListItems
                                   {
                                       ItemID = li.ItemID,
                                       ItemName = li.ItemName
                                   }).ToList()
                    }).ToList();

它编译得很好,对我来说,它看起来应该有效,但没有骰子......任何想法? 提前谢谢

1 个答案:

答案 0 :(得分:1)

不幸的是,EF不支持您修改块选择范围内的值。因此,您可以创建查询,执行它并以映射对象的列表格式获取结果,然后执行转换为ViewModel,DTO等...尝试这样的事情:

List<FullList> x = (from l in entities.Lists
                   select l) // create your query
                   .ToList() // execute query and get the List<Entity>
                   .Select(l => new FullList // customize your result (convert to List<FullList>)
                                {
                                  ListID = l.ListID,
                                  ListName = l.ListName,
                                  ListItems = l.ListItems.Select(li => new ListItems
                                       {
                                           ItemID = li.ItemID,
                                           ItemName = li.ItemName
                                       }).ToList()
                                }).ToList();