System.Linq.Dynamic 动态查询
- 安装
- VS->工具栏->NuGet程序管理器,System.Linq.Dynamic
- 注意:
使用动态查询必须先调用AsQueryable()方法,因为动态扩展仅适用于实现IQueryable的集合。然后迭代结果与常规Linq一样。
- 动态查询
using System.Linq.Dynamic
/// 得到明细过滤条件
/// </summary>
/// <returns></returns>
private string GetFilterWhere(out List<object> paramList)
{
StringBuilder strWhere = new StringBuilder();
object items;
paramList = new List<object>();
strWhere.AppendFormat("ProductionDate >=@{0}", paramList.Count);
paramList.Add(Convert.ToDateTime(startDetailDateEdit.EditValue).Date); strWhere.AppendFormat(" && ProductionDate <=@{0}", paramList.Count);
paramList.Add(Convert.ToDateTime(endDetailDateEdit.EditValue).Date); items = factoryDetailCheckedComboBoxEdit.Properties.GetCheckedItems();
if (items != null && items.ToString() != string.Empty)
{//数组参数,数组中是否包含实例相等的FactoryID值,“outerIt 代表实例”
strWhere.AppendFormat(" && @{0}.Contains(outerIt.FactoryID)", paramList.Count);
paramList.Add(UIHelper.ToArray(items.ToString()));
} if (cbCustomer.HasValue())
{
strWhere.AppendFormat(" && CustomerID=@{0}", paramList.Count);
paramList.Add(Convert.ToInt32(cbCustomer.SelectedValue));
} if (txtMachineCode.Text.Trim() != string.Empty)
{
strWhere.AppendFormat(" && MachineCode.Contains(@{0})", paramList.Count);
paramList.Add(txtMachineCode.Text.Trim());
} return strWhere.ToString();
} //查询
List<object> paramList;
string strWhere = GetFilterWhere(out paramList); //根据条件查询
var query = sourceList.AsQueryable<FacilityEfficiencyReportInfo>().Where(strWhere, paramList.ToArray());
var resultList = (from dynamic g in query select g).ToList();- 动态分组
/// <summary>
/// 得到分组的列名字符串
/// </summary>
/// <returns></returns>
private string GetGroupNames()
{
StringBuilder groupName = new StringBuilder();
if (m_FirstGroupFieldName != null)
{
groupName.Append(m_FirstGroupFieldName.Key);
}
if (m_SecondGroupFieldName != null)
{
if(groupName.Length>)
{
groupName.AppendFormat(",{0}", m_SecondGroupFieldName.Key);
}
else
{
groupName.Append(m_SecondGroupFieldName.Key);
}
}
if (m_ThirdGroupFieldName != null)
{
if(groupName.Length>)
{
groupName.AppendFormat(",{0}", m_ThirdGroupFieldName.Key);
}
else
{
groupName.Append(m_ThirdGroupFieldName.Key);
}
}
return groupName.ToString();
}object value;
string groupNames = GetGroupNames();
List<FacilityEfficiencyReportInfo> actualGroupList = new List<FacilityEfficiencyReportInfo>();//实际组列表,不区分日期
if (string.IsNullOrEmpty(groupNames))
{
actualGroupList = groupList.GroupBy(m => m.ID).Select(g => new FacilityEfficiencyReportInfo { FinishedAmount = g.Sum(m => m.FinishedAmount), Workers = g.Sum(m => m.Workers), Duration = g.Sum(m => m.Duration), Sam = g.Average(m => m.Sam),List=g.ToList() }).ToList();
}
else
{
var query = groupList.AsQueryable()
.GroupBy(string.Format("new ({0})", groupNames), "it")
.Select("new(it.Key as Key, it as GroupList)");
var tempGroupList = (from dynamic g in query select g).ToList();
foreach (var g in tempGroupList)
{//每一组合并成一个FacilityEfficiencyReportInfo实例,明细放入model.List列表
FacilityEfficiencyReportInfo newModel = null;
object test = g.Key;
foreach (FacilityEfficiencyReportInfo m in g.GroupList)
{
if (newModel == null)
{
newModel = m.Clone();
newModel.InitDate();
newModel.List = new List<FacilityEfficiencyReportInfo>();
}
newModel.List.AddRange(m.List);
}
actualGroupList.Add(newModel);
}
}query = Contact.GetContactsList().AsQueryable().Where("@0.Contains(outerIt.Country)", newList<String>() { "Austria", "Poland" });其中“outerIt”代表整理上下文:“联系人”列表。为此,我们应该定义一个“outerIt”关键字
query = Contact.GetContactsList().AsQueryable().Where("@0.Contains(outerIt.Country) && it.BirthDate.Year > @1", new List<string>() { "Austria", "Poland" }, );
System.Linq.Dynamic 动态查询的更多相关文章
- System.Linq.Dynamic的使用
项目中经常用到组合条件查询,根据用户配置的查询条件进行搜索,拼接SQL容易造成SQL注入,普通的LINQ可以用表达式树来完成,但也比较麻烦.有个System.Linq.Dynamic用起来比较方便. ...
- System.Linq.Dynamic 和Nhibernate
var session = NHibernateSessionManager.Instance.GetSession(); "); var staffList = session.Query ...
- System.Linq.Dynamic
http://dynamiclinq.codeplex.com/ 10万回 用动态表达式 0.19s ,普通Lamba 0.02s,效率还可以 /* User: Peter Date: 2016/4/ ...
- EF Core中关于System.Linq.Dynamic.Core的使用(转载)
项目中经常用到组合条件查询,根据用户配置的查询条件进行搜索,拼接SQL容易造成SQL注入,普通的LINQ可以用表达式树来完成,但也比较麻烦.有个System.Linq.Dynamic.Core用起来比 ...
- Cookies 初识 Dotnetspider EF 6.x、EF Core实现dynamic动态查询和EF Core注入多个上下文实例池你知道有什么问题? EntityFramework Core 运行dotnet ef命令迁移背后本质是什么?(EF Core迁移原理)
Cookies 1.创建HttpCookies Cookie=new HttpCookies("CookieName");2.添加内容Cookie.Values.Add(&qu ...
- System.Linq.Dynamic字符串转委托
以前一直想着有没有一个方法能够把字符串直接转化成函数的,刚好有需求就找了下,还真有. 微软地址:https://docs.microsoft.com/en-us/previous-versions/b ...
- 【转】借助System.Linq.Dynamic, IQueryable根据排序字符串排序
在使用Entity Framework时,若有多个排序,需要OrderBy (OrderByDescending)再ThenBy (ThenByDescending) 假设需要根据Name升序排序,再 ...
- 借助System.Linq.Dynamic, IQueryable根据排序字符串排序
在使用Entity Framework时,若有多个排序,需要OrderBy (OrderByDescending)再ThenBy (ThenByDescending) 假设需要根据Name升序排序,再 ...
- EF 6.x、EF Core实现dynamic动态查询和EF Core实现多个上下文实例池你了解多少?
前言 很长一段时间没有写博客了,今天补上一篇吧,偶尔发现不太愿意写博客了,太耗费时间,不过还是在坚持当中,毕竟或许写出来的东西能帮到一些童鞋吧,接下来我们直奔主题.无论是在在EF 6.x还是EF Co ...
随机推荐
- 禁用LinkButton的方法
1.服务器端,使用Enabled属性即可 <asp:LinkButton ID="lbtn" runat="server" Enabled="f ...
- 【BZOJ】3203: [Sdoi2013]保护出题人(几何+三分+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=3203 wa无数次QAQ,犯sb错....一是数组没有引用...二是输出转成了int(越界了sad). ...
- 【vijos】1781 同余方程(拓展欧几里得)
https://vijos.org/p/1781 学习了下拓欧.. 求exgcd时,因为 a*x1+b*y1=a*x2+b*y2=b*x2+(a-b*[a/b])*y2 然后移项得 a*x1+b*y1 ...
- NDK版本 下载地址
最新版本r16 https://dl.google.com/android/repository/android-ndk-r16-windows-x86.zip https://dl.google.c ...
- 一行代码解决各种IE兼容问题,IE6,IE7,IE8,IE9,IE10 (转)
x-ua-compatible 用来指定IE浏览器解析编译页面的model x-ua-compatible 头标签大小写不敏感,必须用在 head 中,必须在除 title 外的其他 meta 之前使 ...
- js for in
JavaScript中for..in循环陷阱 大家都知道在JavaScript中提供了两种方式迭代对象: (1)for 循环: (2)for..in循环: 使用for循环进行迭代数组对象,想必 ...
- 详解ASP.NET提取多层嵌套json数据的方法
本篇文章主要介绍了ASP.NET提取多层嵌套json数据的方法,利用第三方类库Newtonsoft.Json提取多层嵌套json数据的方法,有兴趣的可以了解一下. 本文实例讲述了ASP.NET利用第三 ...
- 努比亚Z18mini多点对焦
25点对焦 分为了中心对焦.中间对焦.边缘对焦三个区域 [参考文献] 手机上感受单反的“多点对焦”努比亚Z18mini给你想象 https://baijiahao.baidu.com/s?id=160 ...
- 一起学android之设置ListView数据显示的动画效果(24)
效果图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGFpX3FpbmdfeHVfa29uZw==/font/5a6L5L2T/fontsize/40 ...
- 面试题思考:IOC的优缺点
先讲重点 面试时怎么答: 先把IOC的概念说出来 依赖注入和控制反转 所谓的依赖注入是甲方开放接口,在它需要的时候,能够将乙方传递进来(注入):所谓的控制反转,甲乙双方不相互依赖,交易活动的进行不依 ...