EFCore 的查询语法是:
Expression<Func<T,bool>> condition = x=>x.ClubId==1 ;
dbContext.Set<T>().Where(condition).ToList();
而我想通过传入一个T实体,根据T被赋值的属性来生成condition,用了反射和表达式树。不知道有没有大神告诉下其他的办法。
service层
public List<PlayersEntity> GetPlayers(PlayersEntity queryModel)
{
//获取类的属性
PropertyInfo[] properties = queryModel.Players.GetType().GetProperties();
BinaryExpression condtion = null;
//Expression<Func<PlayersEntity, bool>> expression;
var param = Expression.Parameter(typeof(PlayersEntity), "x"); //x=>
//遍历属性
foreach (PropertyInfo property in properties)
{
var value = property.GetValue(queryModel);
//空值判断
if (value