How to convert LINQ query data to WPF/MVVM List

c# devexpress-wpf entity-framework-6 linq mvvm

Question

I have two entities:

 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; }
    }

In ViewModel I created two lists from these two entities:

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

And I need to construct third list for my WPF/MVVM form with data from these two lists: Structure I need is:

OwnerTransportID
OwnerID
TransportID
Model
Brand

What do I do next in my ViewModel.cs? Do I need to create new class?

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; }
}

and then

List<OwnerTransport> OwnerTransportTransportList 

?? Could You help?

Data I get from linq query:

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

So I have data, and don't know how to convert it to List<OwnerTransportTranspot>.

1
0
6/17/2016 7:26:47 AM

Accepted Answer

Well you can actually do this:

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; }
}
1
6/17/2016 5:50:01 AM

Popular Answer

According to user3185569 answer, I wrote this:

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

I have no mistakes at least, but compearing this code to foreach, it tooks so long :( but this answer was what I needed! I will accept it



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow