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”。
请告诉我我的错误在哪里
答案 0 :(得分:2)
select new Comment
{
u.Location //<- remove the u.
答案 1 :(得分:2)
u.Location
应为Location
。
答案 2 :(得分:0)
试试这个
var comment = (from u in context.Comments
where u.UserID == userId
select new Comment
{
Location = context.Locations.FirstOrDefault(x=>x.LocationID==u.LocationID).Name,
CommentFor = "Bb",
DateAdded = u.DateAdded,
CommentText = u.CommentText
}
).ToList();