gpt4 book ai didi

c# - 从 linq 中的多个连接设置字段值

转载 作者:太空宇宙 更新时间:2023-11-03 23:24:49 25 4
gpt4 key购买 nike

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

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

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

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
}

关于c# - 从 linq 中的多个连接设置字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34201804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com