我有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
我玩过但没有成功......
答案 0 :(得分:3)
看起来你非常接近。假设查询的其余部分没问题,您应该可以通过选择所需的字段来创建匿名类型,如下所示:
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
}