Linq to Entities...

  • Thread starter Thread starter Pipo
  • Start date Start date
P

Pipo

Hi,

I have 2 entities, ProjectUsers and Users.
I must populate 3 listboxes
-Users
-ProjectUsers
-AdminUsers

The last 2 listboxes I can fill, no problem(ProjectUsers.Admin ==
true/false)
But....how can I filter the users entity to give me all the users that are
not already in the ProjectUsers entity??


tia,

Pipo
 
Pipo said:
Hi,

I have 2 entities, ProjectUsers and Users.
I must populate 3 listboxes
-Users
-ProjectUsers
-AdminUsers

The last 2 listboxes I can fill, no problem(ProjectUsers.Admin ==
true/false)
But....how can I filter the users entity to give me all the users that are
not already in the ProjectUsers entity??

Something like this should work:
Users.Where(u => !ProjectUsers.Any(p => p.Name == u.Name))

Michael
 
Hi,

I have 2 entities, ProjectUsers and Users.
I must populate 3 listboxes
-Users
-ProjectUsers
-AdminUsers

The last 2 listboxes I can fill, no problem(ProjectUsers.Admin ==
true/false)
But....how can I filter the users entity to give me all the users that are
not already in the ProjectUsers entity??

Do you have two _entity sets_ here, or two different _entity types_,
between Users and ProjectUsers?

If it's the first one, and type is the same, then do:

Users.Except(ProjectUsers)

If these are two different entity types, then you have to define how
to compare them for being "the same". Michael offered one way of doing
so by comparing names, but names occasionally match...
 
Pavel Minaev said:
Users.Except(ProjectUsers)

I keep forgetting about that one
If these are two different entity types, then you have to define how
compare them for being "the same". Michael offered one way of doing
o by comparing names, but names occasionally match...

Naturally I was assuming Pipo would substitute the correct check. With
Except he will need to override GetHashCode and Equals to get it to match
correctly. For example, if Name is what we match on then

override int GetHashCode()
{
return this.Name.GetHashCode();
}

override bool Equals(object other)
{
return (other.Name == this.Name);
}
 
Naturally I was assuming Pipo would substitute the correct check. With
Except he will need to override GetHashCode and Equals to get it to match
correctly. For example, if Name is what we match on then

override int GetHashCode()
{
   return this.Name.GetHashCode();

}

override bool Equals(object other)
{
      return (other.Name == this.Name);
}

Subject of the thread seems to imply that this is Entity Framework; in
which case I'd expect he'll just get that Enumerable.Except call
translated to SQL EXCEPT operator directly.
 
Pavel Minaev said:
Subject of the thread seems to imply that this is Entity Framework; in
which case I'd expect he'll just get that Enumerable.Except call
translated to SQL EXCEPT operator directly.

Ah yes, I did read that but read it as "to object" for some reason.
 
Back
Top