2

我在使用以下 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
                                                };

在此处输入图像描述

4

4 回答 4

3

代替

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
            };
于 2013-05-17T05:48:20.653 回答
1

您是否使用 EntityFramework 来检索数据?如果是这样,那么你可以改变你的模型

 

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 int AddressId { get; set; }

        public virtual Address Addresses { get; set; } 

    }

 

EntityFramework 将为您加载地址数据。

于 2013-05-17T06:05:12.663 回答
1

这个设计对我来说有点奇怪,有一个总是只包含一个项目的地址列表,但如果你确实需要这个,你可以使用以下内容进行查询:

    var addcli = from tbcli in dc.tblAdvanceClients
                join tbadd in dc.tblAddresses
                on tbcli.AddressID equals tbadd.AddressID into addrList
                select new AdvanceClient
                {
                    ClientID = tbcli.ClientID,
                    Company = tbcli.Company,
                    Fax = tbcli.Fax,
                    Mobile = tbcli.Mobile,
                    Name = tbcli.Mobile,
                    Telephone = tbcli.Telephone,
                    Addresses = from row in addrList
                                select new Address 
                                {
                                    addressID = row.AddressID, 
                                    address = row.Address 
                                }
                };
于 2013-05-17T06:44:47.317 回答
0

如果我理解不正确,我很抱歉,但我认为应该如下,因为地址和客户表通过 AddressID 具有一对一的关系

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

    //It can has only one address
    public Address Address { get; set; } 

}

并查询:

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,
                                        //tbadd is the address which you are looking for
                                        Addresses = tbadd;
                                    };
于 2013-05-17T06:02:08.243 回答