Linq动态传递数据库中某张表的列

时间:2021-10-18 13:47:56

有些时候,我们需要将数据库中某张表的列作为参数,动态传入到linq中,但是在linq查询语句中并没有这样的功能,这个时候我们会很烦恼。接下来我将通过一下的例子来总结一下有关linq动态传递参数的问题

 

首先我们要引用命名空间

using System.Linq.Expressions;

我们的目标是动态传递参数,此时我们需要用到的ParameterExpression,用它来定义我们所需要的类型及相应的名称

如ParameterExpression para = Expression.Parameter(typeof(Categories), "c");

由于动态传递的参数可能会很多此时我们需要如下操作

List<ParameterExpression> paras = new List<ParameterExpression>();
List<Expression> expression = new List<Expression>();

foreach (var c in conditons)
{
Expression left = Expression.Property(para, typeof(Categories).GetProperty(c.Key));//这里获取的是列,所以我们需要通过反射获取数据表中的列
Expression right = Expression.Constant(c.Value);//(创建关联)获取相应字段的值
Expression ex = Expression.Equal(left, right);//放在一个对等条件中

if (expression.Any())
expression[0] = Expression.AndAlso(expression[0], ex);//这样做的目的是:当传入的列有多个时,需要在生成的SQL后面拼接,
else
expression.Add(ex);
}
paras.Add(para);
var conditional = Expression.Lambda<Func<Categories, bool>>(expression[0], paras.ToArray());//设置where字句的表达式
cata = cata.Where(conditional);

具体实现如下

我使用的是linq to entity来加载数据的

public partial class 动态传参
{
NorthwindEntities context;
public 动态传参()
{
context = new NorthwindEntities();
}

public List<Categories> getCategory()
{

List<Categories> categoryList=new List<Categories>();
var data = from c in context.Categories
select c;
if (data != null)
{
foreach (var d in data)
{
Categories category = new Categories();
category.类别编号 = d.类别编号;
category.类别名称 = d.类别名称;
category.说明 = d.说明;
category.图片 = d.图片;

categoryList.Add(category);
}
}
return categoryList;
}


public List<Categories> getCatagoryByConditions(Dictionary<string, string> conditons)
{
List<Categories> catagoryList = new List<Categories>();
ParameterExpression para = Expression.Parameter(typeof(Categories), "c");


var cata = from p in context.Categories
select p;

if (conditons.Any())
{
List<ParameterExpression> paras = new List<ParameterExpression>();
List<Expression> expression = new List<Expression>();

foreach (var c in conditons)
{
Expression left = Expression.Property(para, typeof(Categories).GetProperty(c.Key));
Expression right = Expression.Constant(c.Value);
Expression ex = Expression.Equal(left, right);

if (expression.Any())
expression[0] = Expression.AndAlso(expression[0], ex);
else
expression.Add(ex);
}

paras.Add(para);
var conditional = Expression.Lambda<Func<Categories, bool>>(expression[0], paras.ToArray());
cata = cata.Where(conditional);
}

//var selector2 = Expression.Lambda<Func<Categories, int>>(Expression.Property(para,), para);
var memberList = cata.Select((group) => new
{
group.类别编号,
group.类别名称,
group.说明,
group.图片
});


if (memberList != null)
{
foreach (var d in memberList)
{
Categories category = new Categories();
category.类别编号 = d.类别编号;
category.类别名称 = d.类别名称;
category.说明 = d.说明;
category.图片 = d.图片;

catagoryList.Add(category);
}
}
return catagoryList;
}
页面调用

if (!IsPostBack)
{
动态传参 d = new 动态传参();

Dictionary<string, string> condions = new Dictionary<string, string>();
int i = 1;
condions.Add("类别名称", "点心");
condions.Add("说明", "甜点心,糖果,甜面包");
List<Categories> catagoryList = d.getCatagoryByConditions(condions);
GridView1.DataSource = catagoryList;
GridView1.DataBind();
}

执行结果
Linq动态传递数据库中某张表的列