我有以下LINQ查询来填充我的模型。
var blogs = (from b in Context.Blogs
select new BlogTreeView
{
Created = EntityFunctions.TruncateTime(b.Created),
Children = (from ba in Context.Blogs
where EntityFunctions.TruncateTime(ba.Created) == EntityFunctions.TruncateTime(b.Created)
select new BlogTitle
{
ID = ba.ID,
Title = ba.Title
})
}).Distinct();
问题是distinct会出现以下错误: “'Distinct'操作不能应用于指定参数的集合ResultType。\ r \ nParameter name:argument”
我也试过这个:
var blogs = (from b in Context.Blogs
select new BlogTreeView
{
Created = EntityFunctions.TruncateTime(b.Created)
}).Distinct();
这只给了我想要的独特日期。
然后我试图在foreach的帮助下将孩子们添加到模型中:
foreach (var item in blogs)
{
item.Children = (from ba in Context.Blogs
where
EntityFunctions.TruncateTime(ba.Created) ==
EntityFunctions.TruncateTime(item.Created)
select new BlogTitle
{
ID = ba.ID,
Title = ba.Title
});
}
但是对于子列表,我的返回值为null。在我的foreach循环中,Children列表具有我想要的值但不在返回字段中。
我做错了什么,为什么第一个查询给了我这个错误?