将2个集合的结果插入到新模型中

时间:2016-01-26 15:51:39

标签: c# wpf

我有一个wpf c#app。 我必须列出集合。 我想加入这两个列表并返回符合条件的结果。 我希望将结果加载到新的列表/模型中。

到目前为止,这是我的代码:

var Results = from j in res
             join c in Customer.GetBaseData() on j.CustomerRef equals c.CustomerRef
             where j.JobStatusRef == jobStatusRef                  
             select new {
                 c.CustomerRef,
                 c.CustomerId,
                 c.Add1,
                 c.Town,
                 c.FName,
                 c.SName,
                 j.DateReq,
                 j.JobId,
                 j.JobRef,
                 j.JobStatus
             };

这是我的目标模特:

public class CustomerJobs
{
    public int JobId { get; set; }     
    public string CustomerRef { get; set; }
    public string DateReq { get; set; }     
    public string JobRef { get; set; }      
    public string JobStatus { get; set; }
    public int CustomerId { get; set; }
    public string SName { get; set; }
    public string FName { get; set; }
    public string Add1 { get; set; }
    public string Town { get; set; }
}

我不知道如何做最后一步?

2 个答案:

答案 0 :(得分:6)

不要使用new { ... }创建匿名类型,而是直接在select:

中使用您的模型
var Results = from j in res
         join c in Customer.GetBaseData() on j.CustomerRef equals c.CustomerRef
         where j.JobStatusRef == jobStatusRef
         // Note the "new CustomerJobs" part.                  
         select new CustomerJobs {
             c.CustomerRef,
             c.CustomerId,
             c.Add1,
             c.Town,
             c.FName,
             c.SName,
             j.DateReq,
             j.JobId,
             j.JobRef,
             j.JobStatus
         };

答案 1 :(得分:3)

在linq中选择时,您可以指定对象的类型,如:

var Results = from j in res
             join c in Customer.GetBaseData() on j.CustomerRef equals c.CustomerRef
             where j.JobStatusRef == jobStatusRef                  
             select new CustomerJobs {
                CustomerRef = c.CustomerRef,
                CustomerId = c.CustomerId,
                Add1 = c.Add1,
                Town = c.Town,
                FName = c.FName,
                SName = c.SName,
                DateReq = j.DateReq,
                JobId = j.JobId,
                JobRef = j.JobRef,
                JobStatus = j.JobStatus
             };