我有一个表格,我想对变量列进行查询。 像:
private void query(string column, string value) {
using (var db = new myDB()) {
var s1 = (from c in db.Components
where (**column** == **value**)
select new {c.id, **column**});
}
}
假设我想寻找供应商,那就像是:
var s1 = (from c in db.Components
where (c.supplier == "abc")
select new {c.id, c.supplier});
有没有办法将列名作为变量传递?
答案 0 :(得分:3)
一个好方法是使用Dynamic Linq
类似的东西:
var s1 = (from c in db.Components
where(column + "=" + value)
select new {c.id, **column**});
答案 1 :(得分:2)
这个例子我觉得很有用。
void BindGridTypeSafe()
{
NorthwindDataContext northwind = new NorthwindDataContext();
var query = from p in northwind.Products
where p.CategoryID == 3 && p.UnitPrice > 3
orderby p.SupplierID
select p;
GridView1.DataSource = query;
GridView1.DataBind();
}
void BindGridDynamic()
{
NorthwindDataContext northwind = new NorthwindDataContext();
var query = northwind.Products
.Where("CategoryID = 3 AND UnitPrice > 3")
.OrderBy("SupplierID");
GridView1.DataSource = query;
GridView1.DataBind();
}
答案 2 :(得分:0)
最简单的答案是添加System.Linq.Dynamic库作为参考并执行以下操作:
string columnName = "Supplier";
var s1 = Suppliers
.Where(String.Format("{0} == \"abc\"", columnName))
.Select(new {c.id, c.supplier};
以下是Dynamic Linq的完整工作示例,其中column-name是参数:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
public class Program
{
public static void Main()
{
var lstContacts = new List<Contact>{
new Contact{Id = 1, Active = true, Name = "Chris"},
new Contact{Id = 2, Active = true, Name = "Scott"},
new Contact{Id = 3, Active = true, Name = "Mark"},
new Contact{Id = 4, Active = false, Name = "Alan"}};
string columnName = "Active";
List<Contact> results = lstContacts.Where(String.Format("{0} == true", columnName)).ToList();
foreach (var item in results)
{
Console.WriteLine(item.Id.ToString() + " - " + item.Name.ToString());
}
}
}
public class Contact
{
public int Id
{
get;
set;
}
public bool Active
{
get;
set;
}
public string Name
{
get;
set;
}
}
您可以尝试使用此.net-fiddle-here
答案 3 :(得分:0)
我要恢复此旧线程,因为今天我不得不使用ASP.NET Core 2.2解决该问题。我使用了System.Linq.Dynamic.Core NuGet包来创建以下扩展方法,如果您需要检查多个给定的列中是否包含多个给定的字符串值,该方法可以很好地工作。
public static IQueryable<TEntity> WhereContains<TEntity>(
this IQueryable<TEntity> query,
string field,
string value,
bool throwExceptionIfNoProperty = false,
bool throwExceptionIfNoType = false)
where TEntity : class
{
PropertyInfo propertyInfo = typeof(TEntity).GetProperty(field);
if (propertyInfo != null)
{
var typeCode = Type.GetTypeCode(propertyInfo.PropertyType);
switch (typeCode)
{
case TypeCode.String:
return query.Where(String.Format("{0}.Contains(@0)", field), value);
case TypeCode.Boolean:
var boolValue = (value != null
&& (value == "1" || value.ToLower() == "true"))
? true
: false;
return query.Where(String.Format("{0} == @0", field), boolValue);
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
return query.Where(String.Format("{0}.ToString().Contains(@0)", field), value);
// todo: DateTime, float, double, decimals, and other types.
default:
if (throwExceptionIfNoType)
throw new NotSupportedException(String.Format("Type '{0}' not supported.", typeCode));
break;
}
}
else
{
if (throwExceptionIfNoProperty)
throw new NotSupportedException(String.Format("Property '{0}' not found.", propertyInfo.Name));
}
return query;
}
该代码可以与.NETStandard / .NETCore(使用上述System.Linq.Dynamic.Core软件包)一起使用,也可以与ASP.NET 4.x(使用System.Linq.Dynamic软件包一起)一起使用。
有关WhereContains扩展方法的更多信息和完整的用例信息,请在我的博客上查看this post。