c# - linq中的sql where子句规则
问题描述
我正在尝试将我的 SQL 规则放在 Linq 中,因为我的规则是从查询生成器生成的,我需要根据规则过滤我的数据,这是我的简单示例
class Program
{
static void Main(string[] args)
{
PromotionVm lObjPromVm = new PromotionVm();
for (int i = 1; i <= 5; i++)
{
PromotionList lObjPromList = new PromotionList();
lObjPromList.active_indicator = 1;
lObjPromList.principle_code = "a" + i;
lObjPromList.promotion_code = "b" + i;
lObjPromList.promotion_plan_number = 20 + i;
lObjPromList.promotion_type_code = 30 + i;
lObjPromList.start_date = DateTime.Now.AddDays(i);
lObjPromVm.promotion_list.Add(lObjPromList);
}
//var sqlRule= "promotion_type_code = 'expensive' AND Category IN('Food', 'Transportation', 'Shopping') AND(PaymentMode = 'Cash' OR PaymentMode = 'Debit Card' OR(Amount = 35))";
var sqlRule = "promotion_type_code = '33'";
// lObjPromVm.promotion_list.ToDataTable()
var lOutlut = lObjPromVm.promotion_list.Where(sqlRule);
}
}
class PromotionVm
{
public List<PromotionList> promotion_list { get; set; }
public PromotionVm()
{
promotion_list = new List<PromotionList>();
}
}
public class PromotionList
{
public string principle_code { get; set; }
public string promotion_code { get; set; }
public int promotion_plan_number { get; set; }
public int promotion_type_code { get; set; }
public DateTime start_date { get; set; }
public int active_indicator { get; set; }
}
我正在尝试使用System.Linq.Dynamic.Core;
但不工作。
谁能建议我如何通过 SQL 规则过滤我的数据?在这里问了同样的问题如何在 linq where 子句中使用字符串? 但是响应给出的内容,它不起作用。
解决方案
我能够解决问题,我只需要转换为AsQueryable()
.
var sqlRule = "promotion_type_code in (31,33) or (promotion_code=\"b2\")";
var lOutlut = lObjPromVm.promotion_list.AsQueryable().Where(sqlRule);
推荐阅读
- python - Python 请求 PUT
- android - 如何更改 Android 12 启动画面中的图标形状?
- postgresql - hyperledger-explorer 无法连接到 IBM Cloud 上已供应的 Postgres 实例
- jquery - jQuery 无法在 ajax 调用中更新 DOM
- python - 计算的布尔列显示列是否是最新的
- laravel-8 - laravel - Blade-ui-kit 中未定义 Pikaday
- javascript - 使用键排除列表展平 json
- python - 在 django-forms 中编辑外键
- react-native - 捏缩放上的 FlatList 与 ScrollView
- powershell - 如何获取 PowerShell 脚本删除文件的时间和日期