如何在LINQ中用变量替换变量?

时间:2014-02-04 16:02:11

标签: asp.net linq

string companyName="ABC";
var query = from q in context.Company where q.CompanyName == companyName select q;

有没有办法用字符串变量替换查询的q.CompanyName部分 那么用于过滤的字段是参数?

我试过

string str1 = "companySize";
string str2 = "q." + str1; 
string companySize = "Mid";
var query = from q in context.Company where str2 == companySize select q; 

没用。 试图让用户选择查询列。

3 个答案:

答案 0 :(得分:2)

Dynamic query with Linq

了解以下两个选项的详情

你可以使用其中一个

  1. 使用动态LINQ库

    以下博客的示例

    string strWhere = string.Empty;
    string strOrderBy = string.Empty;
    
    if (!string.IsNullOrEmpty(txtAddress.Text))
        strWhere = "Address.StartsWith(\"" + txtAddress.Text + "\")"; 
    if (!string.IsNullOrEmpty(txtEmpId.Text))
    {
        if(!string.IsNullOrEmpty(strWhere ))
            strWhere = " And ";
        strWhere = "Id = " + txtEmpId.Text;
    }
    if (!string.IsNullOrEmpty(txtDesc.Text))
    {
        if (!string.IsNullOrEmpty(strWhere))
            strWhere = " And ";
        strWhere = "Desc.StartsWith(\"" + txtDesc.Text + "\")";
    }
    if (!string.IsNullOrEmpty(txtName.Text))
    {
        if (!string.IsNullOrEmpty(strWhere))
            strWhere = " And ";
        strWhere = "Name.StartsWith(\"" + txtName.Text + "\")";
    }
    
    EmployeeDataContext edb = new EmployeeDataContext();
    var emp = edb.Employees.Where(strWhere);
    
  2. Predicate Builder

  3. 示例

        var predicate = PredicateBuilder.True<employee>();
    
        if(!string.IsNullOrEmpty(txtAddress.Text))
            predicate = predicate.And(e1 => e1.Address.Contains(txtAddress.Text));
        if (!string.IsNullOrEmpty(txtEmpId.Text))
            predicate = predicate.And(e1 => e1.Id == Convert.ToInt32(txtEmpId.Text));
        if (!string.IsNullOrEmpty(txtDesc.Text))
            predicate = predicate.And(e1 => e1.Desc.Contains(txtDesc.Text));
        if (!string.IsNullOrEmpty(txtName.Text))
            predicate = predicate.And(e1 => e1.Name.Contains(txtName.Text));
    
        EmployeeDataContext edb= new EmployeeDataContext();
        var emp = edb.Employees.Where(predicate);
    

答案 1 :(得分:1)

如果你不想使用像dynamicLINQ这样的库,你可以自己创建表达式树:

string str1 = "companySize";
string str2 = "q." + str1; 
string companySize = "Mid";

var param = Expression.Parameter(typeof(string));
var exp = Expression.Lambda<Func<Company, bool>>(
              Expression.Equal(
                  Expression.Property(param, str1),
                  Expression.Constant(companySize)),
              param);

var query = context.Company.Where(exp);

答案 2 :(得分:1)

我认为最好的方法是使用内置库(和PropertyDescriptor类型)。

using System.ComponentModel;

void Main()
{
   Test test = new Test();
   test.CompanyName = "ABC";

   object z = TypeDescriptor.GetProperties(test).OfType<PropertyDescriptor>()
                  .Where(x => x.Name == "CompanyName").Select(x => x.GetValue(test)).FirstOrDefault();

   Console.WriteLine(z.ToString());
}

public class Test
{
   public string CompanyName { get; set; }
}