From: shapper on
Hello,

I have the following list:

IList<Int32> roles = { 1, 2, 3 }

I need to get all users where in user.Roles there is at least one Role
which is id 1, 2 OR 3.
var a = users.Where(u => u.Roles. ??? );

How can I do this?

Thanks,
Miguel
From: Martin Honnen on
shapper wrote:

> I have the following list:
>
> IList<Int32> roles = { 1, 2, 3 }
>
> I need to get all users where in user.Roles there is at least one Role
> which is id 1, 2 OR 3.
> var a = users.Where(u => u.Roles. ??? );

users.Where(u => u.Roles.Any(r => roles.Contains(r)))
assuming u.Roles is IEnumerable<int>. If it is an object with an id
property of type int then maybe
users.Where(u => u.Roles.Any(r => roles.Contains(r.id)))


--

Martin Honnen --- MVP Data Platform Development
http://msmvps.com/blogs/martin_honnen/
From: shapper on
On May 21, 6:08 pm, Martin Honnen <mahotr...(a)yahoo.de> wrote:
> shapper wrote:
> > I have the following list:
>
> > IList<Int32> roles = { 1, 2, 3 }
>
> > I need to get all users where in user.Roles there is at least one Role
> > which is id 1, 2 OR 3.
> > var a = users.Where(u => u.Roles. ??? );
>
>    users.Where(u => u.Roles.Any(r => roles.Contains(r)))
> assuming u.Roles is IEnumerable<int>. If it is an object with an id
> property of type int then maybe
>    users.Where(u => u.Roles.Any(r => roles.Contains(r.id)))
>
> --
>
>         Martin Honnen --- MVP Data Platform Development
>        http://msmvps.com/blogs/martin_honnen/

This is a little bit confusing and I keep having some errors... I
tried the following:

var a = users.Where(u => u.Roles.Any(r => r.Id == 1));

Basically I am trying to get all users that have a Role with Id = 1.

And I get the error:
base {System.SystemException} = {"Member access 'Int32 Id' of 'Role'
not legal on type 'System.Collections.Generic.IList`1[Role]."}

Any idea ... It compiles but then I get this error.

Thanks,
Miguel

And I get
From: shapper on
On May 21, 6:31 pm, shapper <mdmo...(a)gmail.com> wrote:
> On May 21, 6:08 pm, Martin Honnen <mahotr...(a)yahoo.de> wrote:
>
>
>
> > shapper wrote:
> > > I have the following list:
>
> > > IList<Int32> roles = { 1, 2, 3 }
>
> > > I need to get all users where in user.Roles there is at least one Role
> > > which is id 1, 2 OR 3.
> > > var a = users.Where(u => u.Roles. ??? );
>
> >    users.Where(u => u.Roles.Any(r => roles.Contains(r)))
> > assuming u.Roles is IEnumerable<int>. If it is an object with an id
> > property of type int then maybe
> >    users.Where(u => u.Roles.Any(r => roles.Contains(r.id)))
>
> > --
>
> >         Martin Honnen --- MVP Data Platform Development
> >        http://msmvps.com/blogs/martin_honnen/
>
> This is a little bit confusing and I keep having some errors... I
> tried the following:
>
> var a = users.Where(u => u.Roles.Any(r => r.Id == 1));
>
> Basically I am trying to get all users that have a Role with Id = 1.
>
> And I get the error:
> base {System.SystemException} = {"Member access 'Int32 Id' of 'Role'
> not legal on type 'System.Collections.Generic.IList`1[Role]."}
>
> Any idea ... It compiles but then I get this error.
>
> Thanks,
> Miguel
>
> And I get

I solved it ... My problem was that u.Roles was returning null.

Thank You,
Miguel