I am using dapper and a lambda expression in order to retrieve data from the database. Instead of building a ton of overloaded expressions, i want to pass in a single database object and have my lambda expression match on the closest or first object found.
public static User GetUser(User pUser)
{
using (IDbConnection connection = new SqlConnection(Connection))
{
return connection.Query<User>("SELECT * FROM dbo.USERS").(m => m == pUser);
}
}
In the example above, you can see I am passing a "User" object into the function, this user object could be 50% of what the exepected object is. for example, if the object had 2 strings, the ID and the users name. But I only knew the users name. I would create a new user as an ref or out param and have the query fill in the missing data.
Thoughts? I could create a ton over overloaded functions with duplated code like GetUserByID, and GetUserByName but that seems redundant.
You need to re-write the query. Kindly provide the structure of dbo.Users table.
Place filter logic inside of query and use table columns to match records instead of whole object.
public static User GetUser(User pUser)
{
using (IDbConnection connection = new SqlConnection(Connection))
{
return connection.Query<User>("Select * FROM dbo.Users where userID = @UserId", pUser).FirstOrDefault();
}
}