如何将从Linq查询获得的两个表中的数据转换为可以以wpf形式填充的列表

本文关键字:转换 数据 列表 wpf 填充 Linq 查询 两个 | 更新日期: 2023-09-27 18:08:09

我不知道如何将LINQ查询转换为具有传输表数据的所有者列表类型,并将其传递给WPF形式(使用MVVM)

数据库结构:

车主有很多车,所以我这样描述关系:

public partial class Transport
    {
        public Transport()
        {
          TransportOwners = new List<TransportOwner>();
        }
        [Key]
        public int TransportID { get; set; }
        public string PlateNo { get; set; }
        public string Brand { get; set; }
        public string Model { get; set; }
        public virtual ICollection<TransportOwner> TransportOwners { get; set; }
    }
public partial class Owner
    {
        [Key]
        public int OwnerID { get; set; }
        public int TransportID { get; set; }
        [ForeignKey("TransportID")]
        public virtual Transport Transport { get; set; }
        [NotMapped]
        public string PlateNo { get; set; }
        [NotMapped]
        public string Brand { get; set; }
        [NotMapped]
        public string Model { get; set; }
    }

在ViewModel中我创建了Owner的列表类型:

    private List<Owner> _haveList;
    public List<Owner> HaveList
    {
        get { return _haveList; }
        set
        {
            if (value != _haveList)
            {
                _haveList = value;
                RaisePropertiesChanged("HaveList");
            }
        }
    }

现在我正在尝试获取数据:

 using (var dbContext = new DataModelContext())
                    {
                        var query = dbContext.Owners.AsQueryable();

    query = query.Where(o => o.OwnerId.Equal(OwnerParameter));
query = query.Select(t => new 
                            {
                                Model = t.Transport.Model,
                                Brand = t.Transport.Brand,
                                PlateNo = t.Transport.PlateNo
                            }).ToList(); 

//这里我看到了我需要的数据(Owner Transport list)

HaveList = query;
'System.Collections.Generic.List<<anonymous type: ... >>' to 'System.Collections.Generic.List<DataModels.Owner>'

如何将从Linq查询获得的两个表中的数据转换为可以以wpf形式填充的列表

在Linq-to-Entities中,您只能投影到匿名类型或常规类。不能投影到现有实体类型

        var result = (from o in query 
                      where o.OwnerID==OwnerParameter
                     select new OwnerModel
                     {
                         Model=o.Transport.Model,
                         Brand=o.Transport.Brand
                     }).ToList();

1 -您应该尝试使用命名对象

HaveList= query.Select(t => new OwnerModel
                        {
                            Model = t.Transport.Model,
                            Brand = t.Transport.Brand,
                            PlateNo = t.Transport.PlateNo
                        }).ToList(); 

2 -您的查询对象创建为IQuerible,然后您尝试将其分配为List

query = query不应该工作。

注意,OwnerModel应该在其中一个属性被修改时触发inotifationevent:)

private List<OwnerModel> _haveList;
    public List<OwnerModel> HaveList
    {
        get { return _haveList; }
        set
        {
            if (value != _haveList)
            {
                _haveList = value;
                RaisePropertiesChanged("HaveList");
            }
        }
    }

我终于有了我需要的东西,谢谢你的帮助

List<Owner> list = DBContext.Owners.Where(to => to.OwnerID == ownerParameter).ToList();
HaveList = list.Select(t => new Owner()
                    {
                        Model = t.Transport.Model,
                        Brand = t.Transport.Brand,
                        PlateNo = t.Transport.PlateNo
                    }).ToList();