0

我有两个实体:

 public class Transport
    {
        [Key]
        public int TransportID {get; set;}
        public string Model { get; set; }
        public string Brand { get; set; }
    }
    public class TransportOwner
    {
        [Key]
        public int TransportOwnerID {get; set;}
        public int OwnerID {get; set;}
        public int TransportID {get; set;}
        [ForeignKey("TransportID")]
        public virtual Transport Transport { get; set; }
    }

在 ViewModel 中,我从这两个实体创建了两个列表:

public List<Transport> TransportList 
public List<OwnerTransport> OwnerTransportList

我需要使用这两个列表中的数据为我的 WPF/MVVM 表单构建第三个列表: 我需要的结构是:

OwnerTransportID
OwnerID
TransportID
Model
Brand

我接下来在 ViewModel.cs 中做什么?我需要创建新课程吗?

public class OwnerTransportTransport
{
    [Key]
    public int OwnerTransportID {get; set;}
    public int TransportID {get; set;}
    public string OwnerID {get; set;}
    public string Model { get; set; }

    public string Brand { get; set; }
}

接着

List<OwnerTransport> OwnerTransportTransportList 

?? 你能帮忙吗?

我从 linq 查询中获得的数据:

var ownerTransportList = OwnerTransport.Select(t => new
{
    t.OwnerTransportID,
    t.OwnerID,
    t.TransportID,
    ModelName = t.Transport.ModelName,
    BrandName = t.Transport.BrandName,
}).ToList();

所以我有数据,不知道如何将其转换为List<OwnerTransportTranspot>.

4

3 回答 3

1

好吧,您实际上可以这样做:

List<OwnerTransportTransport> ListIWantToShow =
                        (from transport in dbContext.TransportOwner
                        select new OwnerTransportTransport()
                        {
                            ID = transport.ID,
                            TransportID = transport.TransportID,
                            BrandName = transport.Transport.BrandName,
                            ModelName = transport.Transport.ModelName
                        }).ToList();


public class OwnerTransportTransport
{
  public int OwnerTransportID { get; set; }
  public int TransportID { get; set; }
  public string Brand { get; set; }
  public string Model { get; set; }
}
于 2016-06-17T05:50:01.363 回答
0

我创建了类:

public class OwnerTransportTransport
{
  public int OwnerTransportID { get; set; }
  public int TransportID { get; set; }
  public string Brand { get; set; }
  public string Model { get; set; }
}

最后是我需要的:)

var query = dbContext.TransportOwner.AsQueryable();
ListIWantToShow = new List<OwnerTransportTransport>();
foreach (var transport in query)
  {
    OwnerTransportTransport NewList = new OwnerTransportTransport
      {
        ID = transport.ID,
        TransportID = transport.TransportID,
        BrandName = transport.Transport.BrandName,
        ModelName = transport.Transport.ModelName
      };

        ListIWantToShow.Add(tranport);
   }
于 2016-06-17T05:11:08.687 回答
0

根据 user3185569 的回答,我这样写:

var query = DBContext.TransportOwner.AsQueryable()
.Where(st => st.OwnerID.StartsWith(someParam));

List<OwnerTransportTransport> ListIWantToShow = query.Select(transport => new OwnerTransportTransport()
                        {
                            ID = transport.ID,
                            TransportID = transport.TransportID,
                            BrandName = transport.Transport.BrandName,
                            ModelName = transport.Transport.ModelName

                        }).ToList();

至少我没有错误,但是将这段代码与foreach进行比较,花了很长时间:(但这个答案是我需要的!我会接受的

于 2016-06-20T06:16:50.077 回答