0

I am having a bit of a technical challenge here.

I have a list which contains object of type User.

Now I have to perform a certain operation using linq where the data coming from two different table must match with an Unique combination of Username and Id.

Something of this kind( this is not a correct implementation)

I am stuck on this implementation for quite sometime now.

Actually i cannot user aUser.Contains() here as it has to be of a primitive type.

Can someone help me with this?

    IList<User> aUser = GetUser(); // this is a cached data

    class User

    {

    string Username;

    Guid id;

    }

    data = (from v in _dataContext.UserValues
    join i in _dataContext.UserItems
    on v.ItemId equals i.Id
    where (aUser.Contains(v.Id) && aUser.Contains(i.UserName)) 
    select v);

thanks

1 Answers1

0

If you change your where clause to the following, it should work...

where (aUser.Select(v => v.id).Contains(v.Id)
    && aUser.Select(v => v.Username).Contains(i.UserName))

By the way, you can improve your Linq if you use method syntax, as you won't need to do the join yourself...

data = _dataContext.UserValues
       .Where(v => aUser.Contains(v.Id)
                && aUser.Contains(v.UserItem.UserName)) 

The exact syntax will depend on how your model is set up, so you might need v.UserItems.UserName at the end, but you should be able to see how to do this. As you can see, the code is much cleaner.

Avrohom Yisroel
  • 8,555
  • 8
  • 50
  • 106