在这篇文章中,我们将为您详细介绍如果LINQ中的列为空,如何忽略“where”和“orderby”条件的内容。此外,我们还会涉及一些关于.net–LINQ中的动态where子句–在运行时可以使用列名、
在这篇文章中,我们将为您详细介绍如果LINQ中的列为空,如何忽略“ where”和“ order by”条件的内容。此外,我们还会涉及一些关于.net – LINQ中的动态where子句 – 在运行时可以使用列名、.net – Order by在LINQ中与Concat()不起作用、c# – LINQ OrderBy / ThenBy带条件排序、c# – 在linq中使用OrderBy()?的知识,以帮助您更全面地了解这个主题。
本文目录一览:- 如果LINQ中的列为空,如何忽略“ where”和“ order by”条件
- .net – LINQ中的动态where子句 – 在运行时可以使用列名
- .net – Order by在LINQ中与Concat()不起作用
- c# – LINQ OrderBy / ThenBy带条件排序
- c# – 在linq中使用OrderBy()?
如果LINQ中的列为空,如何忽略“ where”和“ order by”条件
我有交易对象的列表,并希望根据当前用户的状态按特定条件对它们进行排序。
我的问题是,为了在where子句中添加条件,首先我需要检查它是否为null或防止null指针异常。这将导致筛选出具有null列的记录(我想将它们包括在列表的底部)。
如果该列为空,如何修改查询,以使其忽略条件(位置和顺序),并仍将其附加到结果集?
这是一个示例查询:
transactions = transactions .Where(t => t.PurchaseRequisition != null && t.Award != null && t.PurchaseRequisition.RequisitionedBy != null) .OrderBy(t => t.Award.ContractNumber). ThenBy(t => ToSafeString(t.Award.ContractNumber)). ThenBy(t => ToSafeString(t.PurchaseRequisition.RequisitionedBy.FullName));public string ToSafeString(string s){ return s ?? String.Empty;}
//我想将PurchaseRequisition或Award为null的记录添加到结果集中。
答案1
小编典典您只需要修改OrderBy
andThenBy
子句:
.OrderBy(t => t.Award == null || t.Award.ContractNumber == null).ThenBy(t => t.Award == null ? "" : ToSafeString(t.Award.ContractNumber)).ThenBy(t => t.PurchaseRequisition == null ? "" : ToSafeString(t.PurchaseRequisition.RequisitionedBy.FullName));
现在,您可以完全删除该Where
子句。
.net – LINQ中的动态where子句 – 在运行时可以使用列名
考虑以下情况:
var query = from c in db.Customers where (c.ContactFirstName.Contains("BlackListed") || c.ContactLastName.Contains("BlackListed") || c.Address.Contains("BlackListed")) select c;
需要根据黑名单进行检查的列/属性仅在运行时可用于我.如何生成这个动态的where子句?
另外一个复杂的问题是,可查询的集合(上面的db.Customers)被输入到“Customer”的基类的Queryable(比如’Person’),因此,如上所述编写c.Address不是一个选项.
解决方法
如果你想在运行时使用Lambda构建查询的方式,但是我建议您使用PredicateBuilder(http://www.albahari.com/nutshell/predicatebuilder.aspx),并具有以下内容:
Expression<Fun<T,bool>> pred = null; //delcare the predicate to start with. Note - I don't kNow your type so I just used T if(blacklistFirstName){ pred = p => p.ContactFirstName.Contains("Blacklisted"); } if(blacklistLastName){ if(pred == null){ pred = p => p.ContactLastName.Contains("Blacklisted"); //if it doesn't exist just assign it }else{ pred = pred.And(p => p.ContactLastName.Contains("Blacklisted"); //otherwise we add it as an And clause } }
对于您要包括的所有列,依此类推.当你得到你的查询,你只需要这样的东西:
var results = db.Customers.Where(pred).Select(c => c);
我已经用它来构建LINQ来搜索大约20个不同的选项,并且它产生了非常好的sql.
.net – Order by在LINQ中与Concat()不起作用
谢谢,
下午
(From items In myDatabase.ItemAssignments _ Where items.BuildingID = buildingID _ And items.ResidentID = ResidentID _ Select items).Concat(From moreitems In myDatabase.ItemAssignments _ Where moreitems.occupied = 0 _ And moreitems.BuildingID = buildingID _ Order by moreitems.Floor,moreitems.ItemNumber _ Select moreitems)
解决方法
DataContext.Log
property,则可以从生成的sql验证这一点.
一种处理这种情况的方法是通过匿名类型引入一个虚拟值来帮助排序.为了清楚起见,我已经分解了下面的查询,尽管使用您开始使用的查询语法可以使用相同的方法,直到您需要指定顺序.
Dim firstQuery = From items In myDatabase.ItemAssignments _ Where items.BuildingID = buildingID _ And items.ResidentID = ResidentID _ Select New With { .Row = items,.Order = 1 } Dim secondQuery = From moreitems In myDatabase.ItemAssignments _ Where moreitems.occupied = 0 _ And moreitems.BuildingID = buildingID _ Select New With { .Row = moreitems,.Order = 2 } Dim query = firstQuery.Concat(secondQuery) _ .OrderBy(Function(o) o.Order) _ .ThenBy(Function(o) o.Row.Floor) _ .ThenBy(Function(o) o.Row.ItemNumber) _ .Select(Function(o) o.Row)
另一个不太可取的选择是在其中一个查询中调用AsEnumerable
method,这将从数据库中提取结果.根据所涉及的项目数量,如果需要进一步过滤,这可能会对性能产生不利影响.
要使用此方法,可以更改原始查询的第一部分以使用:
From items In myDatabase.ItemAssignments.AsEnumerable() ...
第二部分的顺序将按照预期的方式工作,生成的sql将反映出来.
c# – LINQ OrderBy / ThenBy带条件排序
例:
01-02-2014 "Description A" 01-02-2014 "Description B" 01-02-2014 "Description C" 01-02-2014 "Description D" 01-02-2014 "Description E" 02-02-2014 "Description A" 02-02-2014 "Description B" 02-02-2014 "Description C" 02-02-2014 "Description D" 02-02-2014 "Description E"
我需要它如何排序是按日期和描述,但也是每个日期内列表顶部的所有描述B元素.像这样,
01-02-2014 "Description B" <-- Top (Rest below is still sorted ascending) 01-02-2014 "Description A" 01-02-2014 "Description C" 01-02-2014 "Description D" 01-02-2014 "Description E" 02-02-2014 "Description B" <-- Top (Rest below is still sorted ascending) 02-02-2014 "Description A" 02-02-2014 "Description C" 02-02-2014 "Description D" 02-02-2014 "Description E"
我试过用LINQ做这个,但我不确定它是否可以作为单个查询完成.
return listofItems.OrderByDescending(x => x.Date).ThenBy(x => x.Type)
解决方法
return listofItems.OrderBy(x => x.Date) .ThenByDescending(x => x.Type == "Description B") .ThenBy(x => x.Type);
c# – 在linq中使用OrderBy()?
所以我做的事情如下:
IEnumerable<ItemType> ordered = myItems.OrderBy( (item) => item.Age);
但是,我想修改myItems集合本身以获得有序序列……是否可能?
我想要的东西:
myItems.someActionHere // This should modify myItems in place.
这可能是一个新手或菜鸟问题.对于那个很抱歉.
解决方法
但是,同样地,您可以将.ToArray()或.ToList()标记到linq语句的末尾,以“实现”有序的可枚举,如果重复引用不是问题(通常不是).
例如:
myItems = myItems.OrderBy(i => i.Age).ToArray();
如果你绝对需要它作为另一个ReadOnlyCollection实例 – 你可以这样做:
myItems = new ReadOnlyCollection<ItemType>(myItems.OrderBy(i => i.Age).ToArray());
注意.ToArray()的工作原理是因为数组实现了IList< T>.即使它们不可修改.
或者 – 也有Array.AsReadOnly – 打开它的头:
myItems = Array.AsReadOnly(myItems.OrderBy(i => i.Age).ToArray());
除了是一个较短的代码行,我没有看到太多的好处 – 除非减少角括号对你很重要:)
今天关于如果LINQ中的列为空,如何忽略“ where”和“ order by”条件的介绍到此结束,谢谢您的阅读,有关.net – LINQ中的动态where子句 – 在运行时可以使用列名、.net – Order by在LINQ中与Concat()不起作用、c# – LINQ OrderBy / ThenBy带条件排序、c# – 在linq中使用OrderBy()?等更多相关知识的信息可以在本站进行查询。
本文标签: