【问题标题】:Entity Framework 4.0 with Linq带有 Linq 的实体框架 4.0
【发布时间】: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


【解决方案1】:

u.Location 应该是Location

【讨论】:

  • 我确实喜欢这个..问题现在解决了。感谢快速重播..但还有其他问题。像这样:无法在 LINQ to Entities 查询中构造实体或复杂类型“CGWeb.Models.Repositories.Comment”。
【解决方案2】:
                   select new Comment
                   {
                       u.Location //<- remove the u.

【讨论】:

  • 好的。这是一个问题。现在解决了......但现在出现错误。无法在 LINQ to Entities 查询中构造实体或复杂类型“CGWeb.Models.Repositories.Comment”。
  • 如果“评论”是您模型中的实体,您将遇到此问题。您必须在投影中使用匿名类型或未映射的类型。
  • 好的..你能具体说明“匿名类型”是什么意思吗..这里需要什么解决方案..?请分享
【解决方案3】:

试试这个

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();

【讨论】:

    猜你喜欢
    • 2017-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-07
    • 2011-05-03
    • 2019-02-17
    • 1970-01-01
    • 2018-11-30
    相关资源
    最近更新 更多