[代码示例]LINQ to Entities查询语句:返回指定的字段

更新: 梦想成为现实:在Enitity Framework中以理想方式实现指定字段查询

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.
posted @ 2011-03-28 15:00  dudu  阅读(5426)  评论(11编辑  收藏  举报