如何将 LINQ 查询数据转换为 WPF/MVVM 列表
How to convert LINQ query data to WPF/MVVM List
我有两个实体:
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 下一步要做什么?我需要创建新的 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; }
}
然后是
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>
。
我创建了 class:
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);
}
其实你可以这样做:
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; }
}
根据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进行比较,花了这么长时间:(但是这个答案正是我所需要的!我会接受它
我有两个实体:
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 下一步要做什么?我需要创建新的 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; }
}
然后是
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>
。
我创建了 class:
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);
}
其实你可以这样做:
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; }
}
根据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进行比较,花了这么长时间:(但是这个答案正是我所需要的!我会接受它