2012-04-10 74 views
1

我有Institution具有Fund列表作为属性的实体。如何过滤属性

我有一个允许的资金单独列表。

我想选择有allowedFunds列表中任何一个可以轻松完成的基金的机构。但是,当我获得机构时,我希望Funds列表也被过滤。

换句话说,我有Institution1Fund1Fund2Fund1也在allowedFunds列表中。我想返回Institution1与仅有Fund1的Funds列表。是否可以使用lambda表达式为EF 4.1编写查询?

// I have allowed funds in a separate list 
    IEnumerable<Fund> allowedFunds; 

    public partial class Institution 
    { 
     public int Id { get; set; } 
     public virtual ICollection<Fund> Funds { get; set; } 
    } 

    public partial class Fund 
    { 
     public int Id { get; set; } 
     public virtual Institution Institution { get; set; } 
    } 

编辑; Oki,这个问题被编辑,这里是另一种解释。如果你看到我的第二条评论(//删除不允许的机构资金)下面的代码,那就是我想要做的。但我在那里返回研究所集并添加逻辑。而不是这样做,我想在移除不允许的资金后返回机构。以下是我的方法。谢谢。

public IEnumerable<Institution> FindInstitutionsForExternalUser(IEnumerable<Fund> allowedFunds) 
    { 
     IQueryable<Institution> query = GetObjectSet(); 
     //Institutions which are connected to allowedFunds 
     if (allowedFunds != null) 
     { 
      IEnumerable<int> fundIds = allowedFunds.Select(fund => fund.Id); 
      query = query.Where(i => i.Funds.Any(o => fundIds.Any(id => id == o.Id))); ; 
     } 
     IEnumerable<Institution> list = query.ToList().OrderBy(a => a.Name); 
     //remove not allowed Funds from institutions 
     foreach (var institution in list) 
     { 
      IEnumerable<Fund> filterdFunds = 
       institution.Funds.Where(fund => allowedFunds.Any(allowedFund => allowedFund.Id == fund.Id)); 
      institution.Funds = filterdFunds.ToList(); 
     } 
     return list; 
    } 
+5

哇,我不能按照你的要求的。你能重构你的问题吗? – Linger 2012-04-10 04:44:20

+2

是的,真的很难得到这个想法。这是你的'allowedFunds'列表吗?举一些例子并说明你是如何尝试过的。 – 2012-04-10 04:58:55

回答

0

尝试类似如下

from i in Institutes from f1 in i.Funds join f2 in AllowedFunds on f1.Id equals f2.Id select f1