LINQ中的条件Where子句

时间:2015-04-02 11:01:06

标签: c# linq

假设我在网格中显示数据,并且我有许多用于过滤数据的文本框。 员工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();               

2 个答案:

答案 0 :(得分:3)

试试这个: -

首先选择数据: -

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);

答案 1 :(得分:2)

如果您想将And运算符和Or运算符混合在一起,请在此处检查PredicateBuilderhttp://www.albahari.com/nutshell/predicatebuilder.aspx

您可以简单地写一下:

// begin with true if you start with And operator.
var predicate = PredicateBuilder.True<TblFamilie>();
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<TblFamilie>();
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<TblFamilie>();
inner = inner.Or (p => p.Name.Contains("foo"));
inner = inner.Or (p => p.Name.Contains("bar"));

var outer = PredicateBuilder.True<TblFamilie>();
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<Family>
{
    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<Family>();
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

你可以复制&amp;粘贴到LinqPad以测试它是如何工作的。在LinqPad中运行之前,

  • 从上方链接下载LinqKit.dll
  • 确保按'F4'&gt;添加&gt;浏览&gt;选择LinqKit.dll&gt;在“其他命名空间导入”选项卡中添加LinqKit命名空间。
  • 在“查询”面板中,选择“语言”为“C#Statement(s)”

粘贴并运行。

// you have 4 strings from DB, API or anywhere.
var strings = new List<string>
{
    "foo",
    "bar",
    "foobar",
    "fake"
};


// make predicate!
// if a string contains "oo" or "ke"
var predicate = PredicateBuilder.True<string>();
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);
}