我有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
}

10-02 01:52
查看更多