我有3個實體:橫幅,包和文件。EntityFramework - 在不加載其他列的情況下將ID加載到級別
One Banner有很多包,一個包有很多文件。
我需要獲取所有橫幅的所有文件ID。我試過如下:
IList<BannerModel> banners = context.Banners
.OrderBy(x => Guid.NewGuid())
.Take(count)
.Select(x =>
new BannerModel {
Images = x.Packs.SelectMany(p => p.Files.Select(f => f.Id)).ToList()
}).ToList();
不過,我對文件的ID選擇得到一個錯誤:
System.NotSupportedException: LINQ to Entities does not recognize the method 'System.Collections.Generic.List
1[System.Int32] ToList[Int32](System.Collections.Generic.IEnumerable
1[System.Int32])' method, and this method cannot be translated into a store expression. at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.DefaultTranslator.Translate(ExpressionConverter parent, MethodCallExpression call) at ...
的東西可能是錯的任何想法?
注意:我禁用了延遲加載。
那豈不是需要一個獨特的(? – 2013-02-28 21:44:30
它爲什麼需要獨特?我肯定知道Files Id是不同的......並且不可能做我做的投影,所以我可以在另一個查詢中定義ImageIds? – 2013-02-28 21:50:31
@Shapper再次同意:)刪除無用的Distinct()。如果您選擇的不是PK,則需要Distinct()。在你的情況下,它不需要它 – 2013-02-28 21:52:31