C#DynamicLinq where子句与Any()

时间:2016-06-10 05:18:46

标签: c# linq dynamic-linq

我想使用字符串where子句运行动态linq:

query = db.Customers.Where("Categories.Any(Code == 'Retail')");

客户实体有类别集合

class Customer
{
    public List<Category> Categories {get;set;}
    ...
}

class Category
{
    public Guid Id {get;set;}
    public string Code {get;set;}
}

任何人都可以告诉我是否可以做这样的事情?

PS:我需要where子句是字符串。 where子句将在运行时生成,因此我无法使用Linq查询表达式。

我正在使用Telerik DataAccess。

5 个答案:

答案 0 :(得分:4)

只要您遵循Expression Language规则,就可以了。

例如,字符串文字必须用双引号括起来:

query = db.Customers.Where("Categories.Any(Code == \"Retail\")");

答案 1 :(得分:2)

您可以构建自己的运行时Expression

Expression<Func<Customer, bool>> myRuntimeExpression = null;

if(condition1)
{
    myRuntimeExpression = cust => cust.Categories.Any(cat => cat.Code == "Retial"); // or some local variable
}
else if(condition2)
{
    myRuntimeExpression = cust => cust.Categories.Any(cat => cat.Id = someId) == false;
}
else if(condition3)
{

}

var query = DB.Customers.Where(myRuntimeExpression);

但是,如果您需要构建更复杂的查询,请查看Dynamic Queries in Linq Using Expressions

答案 2 :(得分:1)

不应该像下面那样吗?

refinedCustomerList = db.Customers.Where(customer => customer.Categories.Any(Code == 'Retail'));

以上列表将包含所有类别为零售&#39;

的客户

答案 3 :(得分:1)

你需要这样的东西:

query =  db.Customers.Where(x => x.Categories.Where(y => y.Code == "Retail").Any());

答案 4 :(得分:1)

linq extention method were接受<?php $sql1= "SELECT * FROM sln_customer_cart WHERE sln_customer_id='".$_SESSION['getincusid']."'"; $result1= mysql_query($sql1); while($row1= mysql_fetch_array($result1)) { ?> <tr> <td class="image hidden-xs"> <?php $sql2="SELECT * FROM sln_vendor_deals WHERE sln_deal_id='".$row1['sln_deal_id']."'"; $result2= mysql_query($sql2); while($row2= mysql_fetch_array($result2)){ echo $row2['sln_deal_name'];} ?> </td> <td class="details"> <div class="clearfix"> <div class="pull-left"> <a href="#" class="title"> <?php echo $row1['sln_offer_name']; ?> </a> </div> <div class="action pull-right"> <div class="clearfix"> <button class="btn-danger btn-raised ripple-effect"> <i class="ti-trash"> </i> </button> </div> </div> </div> </td> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script type="text/javascript"> $('.qtyin, .pricein').on('input', function() { var row = $(this).closest("tr"); var qty = parseInt(row.find('.qtyin').val()); var price = parseFloat(row.find('.pricein').val()); row.find('.totalout').val((qty * price ? qty * price : 0).toFixed(2)); }); </script> <td class="qty"> <input type='text' name='qty' class="qtyin" id='qty' value='1' /> </td> <td class="unit-price hidden-xs"> <span class="currency"> Rs </span> <input type='text' name='price' class="pricein" id='price' value='<?php echo $row1['sln_offer_price'] ?>' /> </td> <td class="total-price"> <span class="currency"> Rs </span> <input type='text' name='total' class="totalout" id='total' value='' /> </td> </tr> <?php } ?>

类型的参数
  • 在您的情况下,System.Func<TSource, Int32, Boolean>收到Func作为参数并返回true / false。
    Where函数的结果将是Customer返回Customers的所有Func

true是一个字符串,而不是"Categories.Any(Code == 'Retail')",因此不能作为参数传递给Func方法。

如果您想保持查询的灵活性,也许您正在寻找的是:

Where

用法:

Public Customer[] QueryCustomers(Func<Customer,bool> predicate)
{
    var result =  db.Customers.Where(c=> predicate(c)).ToArray();
    return result;
}

或者您可能在运行/编译时编写的任何其他查询:

var retailCustomers =
      QueryCustomers(customer => customer.Categories.Any(Code == 'Retail'))