动态 Linq 查询有帮助吗?

发布于 12-11 12:47 字数 751 浏览 0 评论 0原文

如何为以下简单搜索条件编写动态 Linq 查询? 1) 学号 2) 姓氏 3) 姓氏和名字

//if (!String.IsNullOrEmpty(StudentNumber))
var results  = (from s in Students              
                where s.StudentNumber == 1001
                select s
               );

//else if (!String.IsNullOrEmpty(LastName) & (String.IsNullOrEmpty(FirstName))

var results  = (from s in Students              
                where s.LastName == "Tucker"
                select s
               );

//else if (!String.IsNullOrEmpty(LastName) & (!String.IsNullOrEmpty(FirstName))            
var results  = (from s in Students              
                where s.LastName == "Tucker" && s.FirstName == "Ron"
                select s
               );   

How do you write a dynamic Linq query for the following simple search criteria?
1) StudentNumber
2) LastName
3) LastName and FirstName

//if (!String.IsNullOrEmpty(StudentNumber))
var results  = (from s in Students              
                where s.StudentNumber == 1001
                select s
               );

//else if (!String.IsNullOrEmpty(LastName) & (String.IsNullOrEmpty(FirstName))

var results  = (from s in Students              
                where s.LastName == "Tucker"
                select s
               );

//else if (!String.IsNullOrEmpty(LastName) & (!String.IsNullOrEmpty(FirstName))            
var results  = (from s in Students              
                where s.LastName == "Tucker" && s.FirstName == "Ron"
                select s
               );   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

反目相谮 2024-12-18 12:47:20

您需要在任何单个查询之外声明您的 results 变量。这将允许您根据不同的条件附加不同的过滤器,并根据需要附加任意数量的过滤器。示例:

var results = Students.AsEnumerable(); // use .AsQueryable() for EF or Linq-to-SQL

if (!string.IsNullorEmpty(StudentNumber)) 
{
    results = results.Where(s => s.StudentNumber.Equals(StudentNumber));
}
else if (!string.IsNullOrEmpty(LastName))
{
    results = results.Where(s => s.LastName.Equals(LastName));

    if (!string.IsNullOrEmpty(FirstName))
    {
         results = results.Where(s => s.FirstName.Equals(FirstName));
         // filter is in addition to predicate against LastName
    }
}

// results can be used here

如果处理 Linq-to-Entities 或 -Sql,请使用 Students.AsQueryable(); 键入初始查询,以便过滤发生在数据库而不是应用程序内部。


有没有办法可以先构造 WHERE 子句并在
不带 if...else 的 Linq 查询

如果您想在查询的第一步之前构建整个 where,这是相同的逻辑。您正在有条件地构建谓词,因此您将涉及某种 if/else。但是,要首先构建整个谓词,您可以针对 Linq to Objects 的 Func 进行构建。

Func<Student, bool> predicate;
if (!string.IsNullOrEmpty(StudentNumber))
{
    predicate = s => s.StudentNumber.Equals(StudentNumber);
}
else if (!string.IsNullOrEmpty(LastName))
{
    predicate = s => s.LastName.Equals(LastName);

    if (!string.IsNullOrEmpty(FirstName))
    {
        Func<Student, bool> p = predicate;
        predicate = s => p(s) && s.FirstName.Equals(FirstName);
    }
}
else
{
    predicate = s => true;
}

var query = Students.Where(predicate);

您会注意到它是完全相同的 if/else 结构。您可以将其分解为复杂的条件表达式,

Func<Student, bool> predicate;
predicate = s =>
    !string.IsNullOrEmpty(StudentNumber)
    ? s.StudentNumber.Equals(StudentNumber)
    : !string.IsNullOrEmpty(LastName)
        ? !string.IsNullOrEmpty(FirstName)
            ? s.LastName.Equals(LastName) && s.FirstName.Equals(FirstName)
            : s.LastName.Equals(LastName)
        : true;

var query = Students.Where(predicate);

但我发现这非常难以理解,当然与更长的 if/else 相比。这个谓词也比我们通过 if/else 构建的谓词更大,因为这个谓词包含所有逻辑,而不仅仅是我们有条件添加的逻辑。

You need to declare your results variable outside of any individual query. This will allow you append different filters based upon your varying criteria, and append as many filters as you need. An example:

var results = Students.AsEnumerable(); // use .AsQueryable() for EF or Linq-to-SQL

if (!string.IsNullorEmpty(StudentNumber)) 
{
    results = results.Where(s => s.StudentNumber.Equals(StudentNumber));
}
else if (!string.IsNullOrEmpty(LastName))
{
    results = results.Where(s => s.LastName.Equals(LastName));

    if (!string.IsNullOrEmpty(FirstName))
    {
         results = results.Where(s => s.FirstName.Equals(FirstName));
         // filter is in addition to predicate against LastName
    }
}

// results can be used here

If dealing with Linq-to-Entities or -Sql, type the initial query with Students.AsQueryable(); so that the filtering happens at the database rather than inside the application.


Is there a way I can construct the WHERE clause first and use it in a
Linq query without if...else

If you want to build the entire where before the first step of the query, it's the same logic. You are conditionally building the predicate, so you will have some sort of if/else involved. However, to build the entire predicate first, you could build against a Func<Student, bool> for Linq to Objects.

Func<Student, bool> predicate;
if (!string.IsNullOrEmpty(StudentNumber))
{
    predicate = s => s.StudentNumber.Equals(StudentNumber);
}
else if (!string.IsNullOrEmpty(LastName))
{
    predicate = s => s.LastName.Equals(LastName);

    if (!string.IsNullOrEmpty(FirstName))
    {
        Func<Student, bool> p = predicate;
        predicate = s => p(s) && s.FirstName.Equals(FirstName);
    }
}
else
{
    predicate = s => true;
}

var query = Students.Where(predicate);

You'll notice it's the exact same if/else structure. You could collapse that down into a complicated conditional expression

Func<Student, bool> predicate;
predicate = s =>
    !string.IsNullOrEmpty(StudentNumber)
    ? s.StudentNumber.Equals(StudentNumber)
    : !string.IsNullOrEmpty(LastName)
        ? !string.IsNullOrEmpty(FirstName)
            ? s.LastName.Equals(LastName) && s.FirstName.Equals(FirstName)
            : s.LastName.Equals(LastName)
        : true;

var query = Students.Where(predicate);

But I find that pretty well difficult to follow, certainly as compared to the longer if/else. This predicate is also bigger than the one we build via the if/else, because this one contains all the logic, it's not just the logic we conditionally added.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文