2013-05-17 95 views
2

我遇到了以下linq查詢的問題。在linq查詢中分配IEnumerable列表

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

+2

怎麼能一個客戶端有多個一個地址,如果地址和客戶端表具有一對一的關係(Client.ClinetID - Client.AddressID - > Address.AddressID)? –

回答

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

你好!實際上tblAdvanceclient中的地址列是不同的,它與我在談論的地址列表無關 – chamara

+1

@chamara,仔細查看您的表格設計,您將擁有一個針對客戶端的單個地址,地址,你會有多個客戶端,這是需求?因爲它看起來不對,應該是另一回事,就像客戶可以/可能有多個地址一樣。 – Habib

0

我很抱歉,如果我沒有理解正確的,但我認爲它應該是如下因爲地址和客戶端表有一個一對一reletionship通過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; 
            }; 
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將爲您加載地址數據。

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