LINQ中的条件Where子句
假设我在网格中显示数据,并且我有许多用于过滤数据的文本框。 员工ID的文本框。 如果employee id文本框为空,则不会添加where子句,但如果它不为空,则会为其添加where子句。 如果工资文本框具有值或员工姓名文本框具有值,我们可以过滤数据。
我尝试编写条件LINQ查询但出错。 这是我的
var sName=""; var r = from t in TblFamilies where 1 == 1 if(sName!="") { && t.Name="Keith"; }; select new { t.ID, t.ParentID, t.Name, t.CurDate }; r.Dump();
试试这个:-
首先选择数据: –
var r = from t in TblFamilie select new { t.ID, t.ParentID, t.Name, t.CurDate };
然后你可以根据条件过滤: –
if (sName!="") r = r.Where(x => x.Name == sName);
如果你想将And
运算符和Or
运算符混合在一起,请在这里查看PredicateBuilder
: http : //www.albahari.com/nutshell/predicatebuilder.aspx
您可以简单地写:
// begin with true if you start with And operator. var predicate = PredicateBuilder.True(); predicate = predicate.And(t => t.CureDate < DateTime.UtcNow.AddDays(-1)); // you can mix with Or operator too. predicate = predicate.Or(t => t.Name.Contains("blah")); var results = context.TblFamilie .Where(predicate) .Select(new { // your projection here... }); // begin with false if you start with Or operator. var predicate2 = PredicateBuilder.False (); predicate2 = predicate2.Or(t => t.CureDate < DateTime.UtcNow.AddDays(-1)); // you can mix with And operator too. predicate2 = predicate2.And(t => t.Name.Contains("blah")); var results = context.TblFamilie .Where(predicate) .Select(new { // your projection here... }); // even nesting is possible var inner = PredicateBuilder.False (); inner = inner.Or (p => p.Name.Contains("foo")); inner = inner.Or (p => p.Name.Contains("bar")); var outer = PredicateBuilder.True (); outer = outer.And (p => p.CureDate > DateTime.UtcNow.AddDays(-3)); outer = outer.And (p => p.CureDate < DateTime.UtcNow.AddDays(-1)); outer = outer.And (inner); var results = context.TblFamilie .Where(outer) .Select(new { // your projection here... });
更新
好的,我们假设你有一个Family
课,你从某个地方得到'家庭'。 您可以像这样使用PredicateBuilder
:
// you have 4 families from DB, API or anywhere. var failies = new List { new Family { Id = 1, ParentId = 1, Name = "foo", Birthday = new DateTime(1971, 1, 1) }, new Family { Id = 1, ParentId = 1, Name = "bar", Birthday = new DateTime(1982, 1, 1) }, new Family { Id = 1, ParentId = 1, Name = "foobar", Birthday = new DateTime(1993, 1, 1) }, new Family { Id = 1, ParentId = 1, Name = "fake", Birthday = new DateTime(2000, 1, 1) }, }; // make predicate! // if a family's Birthday is before than 1980 'or' Name contains "ke". var predicate = PredicateBuilder.True (); predicate = predicate.And(o => o.Birthday < new DateTime(1980, 1, 1)); predicate = predicate.Or(o => o.Name.Contains("ke")); // you should make IQueryable in order to use PredicateBuilder. var result = failies.AsQueryable() .Where(predicate) .Select(o => new { o.Id, o.Name, o.Birthday // only project what you want. }) .ToList(); // now, result should contains "foo" and "fake". foreach (var family in result) { Debug.WriteLine("Name: " + family.Name); }
Updated2
您可以复制并粘贴到LinqPad以测试它的工作原理。 在LinqPad中运行之前,
- 从上面的链接下载
LinqKit.dll
。 - 确保按'F4'>添加>浏览>选择LinqKit.dll>在“其他命名空间导入”选项卡中添加
LinqKit
命名空间。 - 在“查询”面板中,选择“语言”为“C#语句”
粘贴并运行。
// you have 4 strings from DB, API or anywhere. var strings = new List { "foo", "bar", "foobar", "fake" }; // make predicate! // if a string contains "oo" or "ke" var predicate = PredicateBuilder.True (); predicate = predicate.And(o => o.Contains("oo")); predicate = predicate.Or(o => o.Contains("ke")); // you should make IQueryable in order to use PredicateBuilder. var result = strings.AsQueryable() .Where(predicate) .ToList(); // now, result should contains "foo", "foobar" and "fake". foreach (var stringResult in result) { Debug.WriteLine("Name: " + stringResult); }