C# 使用linq从具有字段名和字段值的列表中选择(类似于select命令)
我想从动态列表中进行选择,其中列表字段名为例如“SecName”,SecName值等于例如“xxx”, 我知道如何在sql中创建它,但我想在实体框架中创建和使用它。 就像在sql中执行字符串一样。 如何在实体框架和linq中实现它。 应该是这样的C# 使用linq从具有字段名和字段值的列表中选择(类似于select命令),c#,sql-server,linq,entity-framework-6,C#,Sql Server,Linq,Entity Framework 6,我想从动态列表中进行选择,其中列表字段名为例如“SecName”,SecName值等于例如“xxx”, 我知道如何在sql中创建它,但我想在实体框架中创建和使用它。 就像在sql中执行字符串一样。 如何在实体框架和linq中实现它。 应该是这样的 var lst=_efmodel.Tables.where(x=>x.fieldname=="SecName" && x.value=="xxx").tolist(); 我正在寻找通过传递两个字符串来过滤列表的语法,第一个字符串
var lst=_efmodel.Tables.where(x=>x.fieldname=="SecName" && x.value=="xxx").tolist();
我正在寻找通过传递两个字符串来过滤列表的语法,第一个字符串应该是属性名,另一个应该是该属性值。代码必须是这样的
var lst=_efmodel.Table.Where(x => x.fieldname == <SomeValue>).ToList();
var lst=\u efmodel.Table.Where(x=>x.fieldname==).ToList();
搜索后,我找到了答案,答案是:
public class SearchField
{
public string Name { get; set; }
public string @Value { get; set; }
//public string Operator { get; set; }
public SearchField(string Name, string @Value)
{
this.Name = Name;
this.@Value = @Value;
//Operator = "=";
}
}
public class FilterLinq<T>
{
public static Expression<Func<T, Boolean>> GetWherePredicate(params SearchField[] SearchFieldList)
{
//the 'IN' parameter for expression ie T=> condition
ParameterExpression pe = Expression.Parameter(typeof(T), typeof(T).Name);
//combine them with and 1=1 Like no expression
Expression combined = null;
if (SearchFieldList != null)
{
foreach (var fieldItem in SearchFieldList)
{
//Expression for accessing Fields name property
Expression columnNameProperty = Expression.Property(pe, fieldItem.Name);
//the name constant to match
Expression columnValue = Expression.Constant(fieldItem.Value);
//the first expression: PatientantLastName = ?
Expression e1 = Expression.Equal(columnNameProperty, columnValue);
if (combined == null)
{
combined = e1;
}
else
{
combined = Expression.And(combined, e1);
}
}
}
//create and return the predicate
if (combined != null) return Expression.Lambda<Func<T, Boolean>>(combined, pe);
return null;
}
}
公共类搜索字段
{
公共字符串名称{get;set;}
公共字符串@Value{get;set;}
//公共字符串运算符{get;set;}
公共搜索字段(字符串名称,字符串@Value)
{
this.Name=Name;
这个@Value=@Value;
//运算符=“=”;
}
}
公共类过滤器LINQ
{
公共静态表达式GetWherePredicate(参数SearchField[]SearchFieldList)
{
//表达式ie T=>条件的“IN”参数
ParameterExpression pe=表达式.参数(typeof(T),typeof(T).Name);
//将它们与and 1=1组合,就像没有表达式一样
表达式组合=空;
if(SearchFieldList!=null)
{
foreach(SearchFieldList中的var fieldItem)
{
//用于访问字段名称属性的表达式
表达式columnNameProperty=Expression.Property(pe,fieldItem.Name);
//要匹配的名称常量
表达式columnValue=表达式.常量(fieldItem.Value);
//第一个表达式:PatientAtlastName=?
表达式e1=表达式.Equal(columnNameProperty,columnValue);
如果(组合==null)
{
组合=e1;
}
其他的
{
组合=表达式和(组合,e1);
}
}
}
//创建并返回谓词
if(combined!=null)返回表达式.Lambda(combined,pe);
返回null;
}
}
然后像这样使用它:
var lst = _efmodel.Count(2015).AsQueryable()
.Where(
FilterLinq<PazTedad_Result>.GetWherePredicate(
new SearchField("FieldName", "FieldValue"))).ToList();
var lst=\u efmodel.Count(2015).AsQueryable()
.在哪里(
FilterLinq.getWhere谓词(
新建搜索字段(“字段名”、“字段值”))。ToList();
这不是一个琐碎的、不可维护的、通常是一个坏主意。考虑重新构建数据库或重构代码,这样就不需要它了。如果你能举个例子说明你为什么需要这个,那会有帮助的。无论如何,请看,等等。它是调用linq的通用模板。我不知道您使用的字段的确切名称。我想用你帖子中的代码提出一些建议。我不确定我的代码是否正确,我想可能应该是这样的,我在寻找正确的语法,我想用两个字符串来过滤我的列表,第一个是列名,第二个是列值。如果你这样想,它会改变一切让我们看看。