Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Filter Properties
    primarykey
    data
    text
    <p>I have <code>Institution</code> entities that have a <code>Fund</code> list as a property.</p> <p>I have a List of allowed Funds separately.</p> <p>I want to select Institutions which have any of the fund from <code>allowedFunds</code> list which can be done easily. But when I get Institutions I want the <code>Funds</code> list also to be filtered.</p> <p>In other words I have <code>Institution1</code> with <code>Fund1</code> and <code>Fund2</code>. <code>Fund1</code> is also in allowedFunds list. I want to return Institution1 with the <code>Funds</code> list having only Fund1. Is it possible to write a query for this with lambda expressions for EF 4.1?</p> <pre><code> // I have allowed funds in a separate list IEnumerable&lt;Fund&gt; allowedFunds; public partial class Institution { public int Id { get; set; } public virtual ICollection&lt;Fund&gt; Funds { get; set; } } public partial class Fund { public int Id { get; set; } public virtual Institution Institution { get; set; } } </code></pre> <p>Edit; Oki,The question is edited and also here is another explanation. If you see the code below my 2nd comment(//remove not allowed Funds from institutions) that is what I want to do. but there I return Institute set and add the logic. Instead of doing that I want to return Institutions after removing not allowed funds.Below is my method. Thanks.</p> <pre><code> public IEnumerable&lt;Institution&gt; FindInstitutionsForExternalUser(IEnumerable&lt;Fund&gt; allowedFunds) { IQueryable&lt;Institution&gt; query = GetObjectSet(); //Institutions which are connected to allowedFunds if (allowedFunds != null) { IEnumerable&lt;int&gt; fundIds = allowedFunds.Select(fund =&gt; fund.Id); query = query.Where(i =&gt; i.Funds.Any(o =&gt; fundIds.Any(id =&gt; id == o.Id))); ; } IEnumerable&lt;Institution&gt; list = query.ToList().OrderBy(a =&gt; a.Name); //remove not allowed Funds from institutions foreach (var institution in list) { IEnumerable&lt;Fund&gt; filterdFunds = institution.Funds.Where(fund =&gt; allowedFunds.Any(allowedFund =&gt; allowedFund.Id == fund.Id)); institution.Funds = filterdFunds.ToList(); } return list; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload