Lambda 如何筛选属性
我有Lambda 如何筛选属性,lambda,Lambda,我有机构实体,这些实体将基金列表作为财产 我有一份单独的允许资金清单 我想从allowedFunds列表中选择拥有任何基金的机构,这样做很容易。但当我得到机构时,我希望基金列表也被过滤 换句话说,我有Institution1和Fund2基金1也在allowedFunds列表中。我想返回Institution1,其中的资金列表中只有Fund1。是否可以使用EF 4.1的lambda表达式为此编写查询 // I have allowed funds in a separate list
机构
实体,这些实体将基金
列表作为财产
我有一份单独的允许资金清单
我想从allowedFunds
列表中选择拥有任何基金的机构,这样做很容易。但当我得到机构时,我希望基金
列表也被过滤
换句话说,我有Institution1
和Fund2
<代码>基金1也在allowedFunds列表中。我想返回Institution1,其中的资金列表中只有Fund1。是否可以使用EF 4.1的lambda表达式为此编写查询
// 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; }
}
//我允许在单独的列表中使用资金
i可数许可基金;
公共部分阶级机构
{
公共int Id{get;set;}
公共虚拟ICollection基金{get;set;}
}
公共部分阶级基金
{
公共int Id{get;set;}
公共虚拟机构{get;set;}
}
编辑;
Oki,这个问题经过编辑,这里还有另一个解释。如果您看到我的第二条评论(//从机构中删除不允许的资金)下面的代码,这就是我想要做的。但在这里,我返回Institute set并添加逻辑。我不想这样做,而是想在移除不允许的资金后返还机构。以下是我的方法。谢谢
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;
}
public IEnumerable FindInstitutions ForExternalUser(IEnumerable allowedFunds)
{
IQueryable query=GetObjectSet();
//与许可基金相关的机构
如果(allowedFunds!=null)
{
IEnumerable fundId=allowedFunds.Select(fund=>fund.Id);
query=query.Where(i=>i.Funds.Any(o=>fundIds.Any(id=>id==o.id));
}
IEnumerable list=query.ToList().OrderBy(a=>a.Name);
//从机构中移除不允许的资金
foreach(列表中的var机构)
{
IEnumerable filterdFunds=
其中(fund=>allowedFunds.Any(allowedFund=>allowedFund.Id==fund.Id));
institution.Funds=filterdFunds.ToList();
}
退货清单;
}
尝试以下方法
from i in Institutes from f1 in i.Funds join f2 in AllowedFunds on f1.Id equals f2.Id select f1
哇,我完全听不懂你的要求。你能重新构造你的问题吗?是的,很难理解。您的允许资金列表是什么?举例说明你是如何尝试的。