Let's say I have a database with 2 tables : User(Id, Name, PetId,...) and Pet(Id, Name, Color,...) and this class :
class UserPet
{
// User table, generated in dbml
public User User {get;}
// Pet table, generated in dbml
public Pet Pet {get;}
public UserPet(User user, Pet pet)
{
User = user;
Pet = pet;
}
[...]
}
I'm trying to make a link to sql method with a filter like
public UserPet[] Get(Expression<Func<UserPet, bool>> criteria)
{
[...]
// System.NotSupportedException: 'The member 'User' has not supported translation to sql'
return (from user in context.User
join pet in context.Pet on usr.PetId equal pet.Id
--> select new UserPet(user, pet).Where(criteria).ToArray()
}
So that, for instance, I can make query with dynamic filtering:
Get(userPet => userPet.Pet.Color == "Red" && userPet.User.Name == "Dave")
Here, the criteria is an object containing 2 tables and I am struggling to use it in the linq to sql method. Any idea ? How to tell Linq To SQL that the properties of UserPet are the tables that were generated in the dbml ?
Thanks !
After some time struggling, I found that the below modification worked (remove the constructor with parameter from UserPet). Why ? Mystery !
UserPet:
class UserPet
{
public User user {get;set;}
public Pet pet {get;set}
}
And method :
public UserPet[] Get(Expression<Func<UserPet, bool>> criteria)
{
[...]
return (from user in context.User
join pet in context.Pet on usr.PetId equal pet.Id
select new UserPet(){User = user, Pet = pet}).Where(criteria).ToArray()
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.