[代码示例]LINQ to Entities查询语句:返回指定的字段
时间:2011-03-28 来源:dudu
using (BlogDbContext context = new BlogDbContext())
{
return (from b in context.BlogConfigs
where b.UserName == loginName
select new { BlogID = b.BlogID, Application = b.Application })
.ToList()
.Select(b => new Blog() { BlogID = b.BlogID, Application = b.Application })
.FirstOrDefault<Blog>();
}
需要注意的是一定要用ToList(),否则会出现下面的异常信息:
System.NotSupportedException : The entity or complex type 'Blog' cannot be constructed in a LINQ to Entities query.
相关阅读 更多 +