2014-03-07 76 views
0
public class Actor 
    { 
     public virtual int Id { get; set; }  
     public virtual string Name { get; set; } 
     public virtual string ShortName { get; set; } 
     public virtual IList<BankAccount> BankAccounts { get; set; } 
    } 
public class BankAccount 
    { 
     public virtual int Id { get; set; } 
     public virtual string IbanPrefix { get; set; } 
     public virtual string Number { get; set; } 
    } 
public class BankAccountModel 
{ 
    public int Id { get; set; } 
    public string IbanPrefix { get; set; } 
    public string Number { get; set; } 
} 
public BankAccountModel[] BankAccounts { get; set; } 

我想使用linq查詢來選擇特定actor ID的數組BankAccounts屬性中的所有bankaccounts。Linq Query for expected result

我已經試過如下

var query = Repository.Query<Actor>().Where(a => a.Id == parameters.ActorId) 
         .Select(a => a.BankAccounts); 

      BankAccounts = query.Select(account => account.Select(a=> 
            new BankAccountModel 
            { 
             Description = a.Description, 
             IbanPrefix = a.IbanPrefix, 
             Id = a.Id, 
             Number = a.Number 
            }).ToArray()); 

但它不是爲我工作。

回答

1
Repository 
.Query<Actor>() 
.Where(a => a.Id == actorId) 
.SelectMany(a => a.BankAccounts) 
.Select(ba => new BankAccountModel 
        { 
         IbanPrefix = ba.IbanPrefix, 
         Id = ba.Id, 
         Number = ba.Number 
        } 
     ); 

如果你想與BankAccounts屬性相匹配ACTROS bankaccounts,嘗試這樣的事情,

Repository 
.Query<Actor>() 
.Where(a => a.Id == actorId) 
.SelectMany(a => a.BankAccounts) 
.Where(ba => BankAccounts.Any(ac => ac.Id == ba.Id)) 
.Select(ba => new BankAccountModel 
        { 
         IbanPrefix = ba.IbanPrefix, 
         Id = ba.Id, 
         Number = ba.Number 
        } 
     );