【发布时间】:2011-08-31 12:31:09
【问题描述】:
public IEnumerable<Models.Comment> GetUserComments()
{
return List<Comment>
{
new Comment
{
CommentFor = "ee",
DateAdded = DateTime.Now,
CommentText = "aaaa",
Location = new Location
{
Name = "Location Name",
Country = new Country
{
Name="Israel"
},
State=new State { Name="TelAviv" }
}
}
};
}
你能帮我纠正一下 Linq 查询吗?
我需要使用 Entity Framework 4 从数据库中获取价值。
我确实喜欢这个
public IEnumerable<Models.Comment> GetUserComments()
{
var comment = (from u in context.Comments
where u.UserID == userId
select new Comment
{
//Location = context.Locations.FirstOrDefault(x => x.locationid == u.LocationID).name,
Location = (from l in context.Locations
where l.LocationID == u.LocationID
select new Location
{
Name = l.Name,
State = (
from s in context.States
where (s.StateID == l.StateID)
select new State { Name = s.Name }
).FirstOrDefault()
}
).FirstOrDefault(),
CommentFor = "bs",
DateAdded = u.DateAdded,
CommentText = u.CommentText
}
).ToList();
}
得到类似的错误:
无法在 LINQ to Entities 查询中构造实体或复杂类型“CGWeb.Models.Repositories.Comment”。
请告诉我我在哪里做错了
【问题讨论】:
-
请花一些时间来正确格式化您的代码(请参阅我的更新)。
标签: linq list c#-4.0 entity-framework-4