如何将System.Data.Entity.Infrastructure.DbQuery类型转换为System.Collections.Generic.List类型?

塞瓦

我有两个简单的清单:

public partial class Visitor
{
    public Visitor()
    {
        this.Visits = new HashSet<Visit>();
    }

    public int Id { get; set; }
    public int PermitId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string MiddleName { get; set; }
    public bool IsValid { get; set; }
    public System.DateTime RegistrationDate { get; set; }
    public byte[] Picture { get; set; }
    public virtual ICollection<Visit> Visits { get; set; }
}

public partial class Visit
{
    public int Id { get; set; }
    public int VisitType { get; set; }
    public System.DateTime VisitDate { get; set; }
    public Nullable<int> Visitor_Id { get; set; }

    public virtual Visitor Visitor { get; set; }
}

在WCF方法中,我编写了查询并试图返回结果:

public List<Visitor> AllVisitors()
        {
            using (var te = new TurnstileDbEntities())
            {
                te.Configuration.ProxyCreationEnabled = false;


                return (List<Visitor>) te.Visitors.SelectMany(visitors => te.Visits
                    .Where(o => o.Visitor_Id == visitors.Id)
                    .DefaultIfEmpty(), (visitors, visit) => new {visitors, visit});
            }
        }

好了,正如我所料,我收到了一个例外:

附加信息:无法将对象“ System.Data.Entity.Infrastructure.DbQuery 1[<>f__AnonymousType02 [TurnstileWcfService.Visitor,TurnstileWcfService.Visit]]”类型转换为“ System.Collections.Generic.List`1 [TurnstileWcfService.Visitor]” 。

好的。我改写这个。宣布新班级。

public class JoinResult
{
    public Visitor Visitors { get; set; }
    public Visit Visits { get; set; }
}

并重写方法。

   public IQueryable AllVisitors()
    {
        using (var te = new TurnstileDbEntities())
        {
            te.Configuration.ProxyCreationEnabled = false;

            return te.Visitors.Join(te.Visits, 
                r => r.Id, a => a.Visitor_Id, (r, a) => new JoinResult
            {
                Visits = a,
                Visitors = r
            });
        }
    }

这是可行的,但我想使用Visitor类。访客类包含ICollectionVisits,我想填充它。在不声明新类的情况下如何在Linq语句中执行此操作?

我也可以在下面的代码中得到想要的东西。但是我不喜欢这样。

var visitors = te.Visitors.ToList();
foreach (var item in visitors)
{
      item.Visits = te.Visits.Where(v => v.Visitor_Id == item.Id).ToList();
}
ry

Gert Arnold是正确的,请尝试:

List<Visitor> visitors = te.Visitors.Include(v => v.Visits).ToList();

编辑我也按如下方式工作:

List<Visitor> visitors = te.Visitors.Include("Visits").ToList();

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何将System.Data.Entity.Infrastructure.DbQuery类型转换为System.Collections.Generic.List类型?

来自分类Dev

合并两个EF查询,无法将类型为System.Data.Entity.Infrastructure.DbQuery的对象转换为System.Collections.Generic.IEnumerable

来自分类Dev

无法将类型'System.Linq.IQueryable'隐式转换为'System.Data.Entity.Infrastructure.DbQuery'

来自分类Dev

将linq转换为模型类失败“无法转换类型'System.Data.Entity.Infrastructure.DbQuery`1的对象”

来自分类Dev

无法将类型为“ System.Data.Entity.DbSet” [City]的对象转换为类型为“ System.Collections.Generic.IEnumerable`1 [System.Web.Mvc.SelectListItem]”的对象

来自分类Dev

传递到字典中的模型项的类型为'System.Data.Entity.Infrastructure.DbQuery

来自分类Dev

无法将类型'System.Collections.Generic.List <System.Data.DataRow>'隐式转换为'System.Collections.Generic.List <string>'

来自分类Dev

将类型'System.Collections.Generic.List <string>'转换为'System.Collections.Generic.IList <>'

来自分类Dev

从 Linq 查询(System.Data.Entity.Infrastructure.DbQuery)获取结果并转换为列表

来自分类Dev

无法将类型“System.Collections.Generic.List<<匿名类型:>>”隐式转换为“System.Collections.Generic.List”

来自分类Dev

无法将类型'System.Collections.IList'隐式转换为'System.Collections.Generic.List

来自分类Dev

无法将类型'System.Collections.Generic.List <AnonymousType#1>'隐式转换为'System.Collections.Generic.List

来自分类Dev

无法将类型System.Collections.Generic.List <>隐式转换为System.Collections.Generic.List <>

来自分类Dev

无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List<Model.Room”

来自分类Dev

无法将类型'System.Collections.Generic.List <string>'转换为'System.Web.Mvc.SelectList'

来自分类Dev

无法将类型'System.Linq.IQueryable'隐式转换为'System.Collections.Generic.List'

来自分类Dev

无法将类型'System.Linq.IQueryable'隐式转换为'System.Collections.Generic.List

来自分类Dev

无法将类型'System.Collections.Generic.List <>'隐式转换为'System.Collections.Generic.IList <>'

来自分类Dev

无法将类型'System.Collections.Generic.List <>'转换为'System.Collections.Generic.IList <>'c#MongoDB

来自分类Dev

无法将类型'System.Collections.Generic.List <>'隐式转换为'System.Collections.Generic.IList <>'

来自分类Dev

错误“无法将类型“System.Collections.Generic.IEnumerable<int>”隐式转换为“System.Collections.Generic.List<int>”

来自分类Dev

无法将类型隐式转换为'System.Collections.Generic.List

来自分类Dev

无法将类型'void'隐式转换为'System.Collections.Generic.List <string>'

来自分类Dev

将值转换为我的JSON类型'System.Collections.Generic.List`时出错

来自分类Dev

无法将类型System.Collections.Generic.List <IEnumerable>隐式转换为<IEnumerable

来自分类Dev

无法将类型'int'隐式转换为'System.Collections.Generic.List <QuickTest.Stock>'

来自分类Dev

无法将类型“ void”隐式转换为“ System.Collections.Generic.List”以用于返回语句

来自分类Dev

无法将类型'System.Collections.Generic.List'隐式转换为'string'

来自分类Dev

将值转换为我的json中的类型'System.Collections.Generic.List`时出错

Related 相关文章

  1. 1

    如何将System.Data.Entity.Infrastructure.DbQuery类型转换为System.Collections.Generic.List类型?

  2. 2

    合并两个EF查询,无法将类型为System.Data.Entity.Infrastructure.DbQuery的对象转换为System.Collections.Generic.IEnumerable

  3. 3

    无法将类型'System.Linq.IQueryable'隐式转换为'System.Data.Entity.Infrastructure.DbQuery'

  4. 4

    将linq转换为模型类失败“无法转换类型'System.Data.Entity.Infrastructure.DbQuery`1的对象”

  5. 5

    无法将类型为“ System.Data.Entity.DbSet” [City]的对象转换为类型为“ System.Collections.Generic.IEnumerable`1 [System.Web.Mvc.SelectListItem]”的对象

  6. 6

    传递到字典中的模型项的类型为'System.Data.Entity.Infrastructure.DbQuery

  7. 7

    无法将类型'System.Collections.Generic.List <System.Data.DataRow>'隐式转换为'System.Collections.Generic.List <string>'

  8. 8

    将类型'System.Collections.Generic.List <string>'转换为'System.Collections.Generic.IList <>'

  9. 9

    从 Linq 查询(System.Data.Entity.Infrastructure.DbQuery)获取结果并转换为列表

  10. 10

    无法将类型“System.Collections.Generic.List<<匿名类型:>>”隐式转换为“System.Collections.Generic.List”

  11. 11

    无法将类型'System.Collections.IList'隐式转换为'System.Collections.Generic.List

  12. 12

    无法将类型'System.Collections.Generic.List <AnonymousType#1>'隐式转换为'System.Collections.Generic.List

  13. 13

    无法将类型System.Collections.Generic.List <>隐式转换为System.Collections.Generic.List <>

  14. 14

    无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List<Model.Room”

  15. 15

    无法将类型'System.Collections.Generic.List <string>'转换为'System.Web.Mvc.SelectList'

  16. 16

    无法将类型'System.Linq.IQueryable'隐式转换为'System.Collections.Generic.List'

  17. 17

    无法将类型'System.Linq.IQueryable'隐式转换为'System.Collections.Generic.List

  18. 18

    无法将类型'System.Collections.Generic.List <>'隐式转换为'System.Collections.Generic.IList <>'

  19. 19

    无法将类型'System.Collections.Generic.List <>'转换为'System.Collections.Generic.IList <>'c#MongoDB

  20. 20

    无法将类型'System.Collections.Generic.List <>'隐式转换为'System.Collections.Generic.IList <>'

  21. 21

    错误“无法将类型“System.Collections.Generic.IEnumerable<int>”隐式转换为“System.Collections.Generic.List<int>”

  22. 22

    无法将类型隐式转换为'System.Collections.Generic.List

  23. 23

    无法将类型'void'隐式转换为'System.Collections.Generic.List <string>'

  24. 24

    将值转换为我的JSON类型'System.Collections.Generic.List`时出错

  25. 25

    无法将类型System.Collections.Generic.List <IEnumerable>隐式转换为<IEnumerable

  26. 26

    无法将类型'int'隐式转换为'System.Collections.Generic.List <QuickTest.Stock>'

  27. 27

    无法将类型“ void”隐式转换为“ System.Collections.Generic.List”以用于返回语句

  28. 28

    无法将类型'System.Collections.Generic.List'隐式转换为'string'

  29. 29

    将值转换为我的json中的类型'System.Collections.Generic.List`时出错

热门标签

归档