Master
Master

Reputation: 2163

Filtering Linq Query with Virtual Icollection Property

I'm trying to query a list producing the meetings where the all people attended does not have an existing business. My query works partially. It works perfectly if there's no one in the meeting that has a business. It'll break if I connect a variety (a person with a business and a person without a business that attends the meeting) May I ask what edits can I make to my ling query so that it'll take into consideration all attendees.

Interaction.Cs

public virtual ICollection<InteractionAttendee> Attendees { get; set; }

Attendee.Cs

public virtual Interaction Interaction { get; set; }

public virtual Person Person { get; set; }

my query

from z in ctx.Meetings
    where z.Attendees.Any(y => !ctx.Businsses.Any(x => x.Owner_Id == y.Person.Id)
select new {Id = z.Id}

Upvotes: 0

Views: 721

Answers (2)

SKG
SKG

Reputation: 152

Not sure if you are looking for this....

You are working with 2 mutually exclusive sets (a person with a business and a person without a business that attends the meeting)

int possibillity = 1; //1=> all has business
                    //2=> none has business
                    //3=> some may have business  
var meetings = 
    ctx.Meetings.where(m=> (possibillity==1 && m.Attendees.All(a=> ctx.Businsses.Any(x => x.Owner_Id == a.Person.Id)))
                    || (possibillity==2 && m.Attendees.All(a=> !ctx.Businsses.Any(x => x.Owner_Id == a.Person.Id))) 
                    || (possibillity==3 && 1==1));

Upvotes: 0

Marc Cals
Marc Cals

Reputation: 2989

Instead of use Any, try with All

from z in ctx.Meetings
    where z.Attendees.All(y => !ctx.Businsses.Any(x => x.Owner_Id == y.Person.Id)
select new {Id = z.Id}

With All you are specifying that all your Attendees must to fullfill the conditions.

Upvotes: 1

Related Questions