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查詢嗎?實體框架4.0與Linq
我需要使用實體框架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();
}
喜歡把自己的錯誤採取從數據庫值:
實體或複雜類型「CGWeb.Models.Repositories .Comment'不能在LINQ to Entities查詢中構造。
請告訴我,我的錯誤,我做了
並請花一些時間來正確地格式化代碼(見我的更新)。 – Steven