繁体   English   中英

在linq中设置多个连接的字段值

[英]setting fields values from multiple joins in linq

我有3个型号:

public class Job
{
    public string CustomerRef {get;set;}
    public string AddressRef {get;set;}
    public string JobStatusRef {get;set;}
    public string CustomerName {get;set;}
    public string AddressTown {get;set;}
    public string JobStatusName {get;set;}
}

public class JobStatus
{
    public string JobStatusRef {get;set;}
    public string JobStatusName {get;set;}
}

public class Customer
{
    public string CustomerRef {get;set;}
    public string AddressTown {get;set;}
}

所有这些模型都填充到List对象中。

我想要一个返回包含字段的列表的方法:

CustomerName;
AddressTown;
JobStatusName;

使用其他2个List对象的值进行设置

我创建了这个连接:

var query_join4 = from j in Data
                  join js in JobStatus.Data
                  on j.JobStatusRef equals js.JobStatusRef
                  join c in Customer.Data
                  on j.CustomerRef equals c.CustomerRef
                  select new
                  {
                     //what goes here??
                  };

此方法必须返回一种List

我玩过但没有成功......

看起来你非常接近。 假设查询的其余部分没问题,您应该能够通过选择所需的字段来创建匿名类型,如下所示:

var query_join4 = from j in Data
                  join js in JobStatus.Data
                  on j.JobStatusRef equals js.JobStatusRef
                  join c in Customer.Data
                  on j.CustomerRef equals c.CustomerRef
                  select new
                  {
                      j.CustomerName,
                      c.AddressTown,
                      js.JobStatusName
                  };

如果你需要传递它以在程序的其他地方使用它,你可能想要创建一个单独的类来存储这些值。

public class CustomerResult
{
    public string CustomerName { get; set; }
    public string AddressTown { get; set; }
    public string JobStatusName { get; set; }
}

...

select new CustomerResult
{
    CustomerName = j.CustomerName,
    AddressTown = c.AddressTown,
    JobStatusName = js.JobStatusName
}

如果您只想返回List<Job> ,则使用该类而不是新类。

select new Job
{
    CustomerName = j.CustomerName,
    AddressTown = c.AddressTown,
    JobStatusName = js.JobStatusName

    // you may want to add the other fields too so your Job class is fully populated
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM