gpt4 book ai didi

c# - 在 linq 查询中分配 IEnumerable 列表

转载 作者:行者123 更新时间:2023-12-02 05:03:39 24 4
gpt4 key购买 nike

我在使用以下 linq 查询时遇到问题。

public class Address
{
public int addressID { get; set; }
public string address { get; set; }
}

public class AdvanceClient
{
public int ClientID { get; set; }
public string Name { get; set; }
public string Mobile { get; set; }

public IEnumerable<Address> Addresses { get; set; }

}

在下面的 linq 查询中,我想将 IEnumerable 地址列表分配给 Addresses 属性。我在 tblAdvanceClient 和 tblAddress 表之间存在一对多关系。

IEnumerable<AdvanceClient> addcli = from tbcli in dc.tblAdvanceClients
join tbadd in dc.tblAddresses
on tbcli.AddressID equals tbadd.AddressID
select new AdvanceClient
{
ClientID = tbcli.ClientID,
Company = tbcli.Company,
Fax = tbcli.Fax,
Mobile = tbcli.Mobile,
Name = tbcli.Mobile,
Telephone = tbcli.Telephone,
Addresses = new Address { } // Here i need to get the list of address to each client
};

enter image description here

最佳答案

代替

Address = new Address { }

改成

Address = tbcli.Addresses //Since you already have a property in AdvanceClient

因此您的查询将是:

IEnumerable<AdvanceClient> addcli = 
from tbcli in dc.tblAdvanceClients
join tbadd in dc.tblAddresses
on tbcli.AddressID equals tbadd.AddressID
select new AdvanceClient
{
ClientID = tbcli.ClientID,
Company = tbcli.Company,
Fax = tbcli.Fax,
Mobile = tbcli.Mobile,
Name = tbcli.Mobile,
Telephone = tbcli.Telephone,
Address = tbcli.Addresses
};

关于c# - 在 linq 查询中分配 IEnumerable 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16602221/

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