如何分离 LINQ to SQL 查询中的条件?

发布于 2024-12-09 20:50:37 字数 273 浏览 0 评论 0 原文

我正在使用 ASP.NET 和 C#,我需要使查询条件动态化, 示例:
数据库:

 Name       Sex
 --------------
 John Doe    M
 Jane Doe    F

现在我想允许对数据集进行搜索,但如果用户选择按姓名或性别进行搜索,则仅查询将允许对姓名或性别列进行搜索。但是,如果用户选择按姓名和性别进行搜索,则查询将允许按姓名和性别列进行搜索。我的问题是我可以使用 if 语句分离查询以解释搜索的动态性质吗?谢谢。

I am using ASP.NET with C# and I need to make the condition of the query dynamic,
Example:
Database:

 Name       Sex
 --------------
 John Doe    M
 Jane Doe    F

Now I want to allow a search on the data set but if the user selects to search by name OR sex only the query will allow a search on the name or sex columns. However if the user selects to search by both name and sex them the query will allow a search by name AND sex column. My question is can I separate the query using if statements to account for the dynamic nature of the search? Thank you.

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

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

发布评论

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

评论(2

失去的东西太少 2024-12-16 20:50:37

您可以这样做:

var q = ...;

if(sex != null) {
    q = q.Where(r => r.Sex == sex);
}
if(name != null) {
    q = q.Where(r => r.Name == name);
}

这将根据传入的内容按性别和/或姓名进行过滤。

q = q.Where(r => r.Sex == sex).Where(r => r.Name == name);

q = q.Where(r => r.Sex == sex && r.Name == name);

You can do:

var q = ...;

if(sex != null) {
    q = q.Where(r => r.Sex == sex);
}
if(name != null) {
    q = q.Where(r => r.Name == name);
}

This will filter by sex and/or name depending on what is passed in.

q = q.Where(r => r.Sex == sex).Where(r => r.Name == name);

is the same as

q = q.Where(r => r.Sex == sex && r.Name == name);
迷爱 2024-12-16 20:50:37

史蒂文是对的,这是一种又好又简单的方法,并且非常适合您的需求。如果您遇到可能有多个条件语句的情况,那么研究动态 linq 是明智的选择。动态 linq 将允许您编写一个查询,例如:

    DatabaseDataContext db = new DatabaseDataContext();

    string WhereClause = string.Empty;

    if (!string.IsNullOrEmpty(Name))
        WhereClause += "Name.ToLower().Contains(\"" + Name.ToLower() + "\") AND ";

    if (!string.IsNullOrEmpty(State))
        WhereClause += "City.State.Name.ToLower().Contains(\"" + State.ToLower() + "\") AND ";

    if (!string.IsNullOrEmpty(County))
        WhereClause += "City.County.Name.ToLower().Contains(\"" + County.ToLower() + "\") AND ";

    if (!string.IsNullOrEmpty(City))
        WhereClause += "City.Name.ToLower().Contains(\"" + City.ToLower() + "\") AND ";


    if (WhereClause.EndsWith(" AND "))
        WhereClause = WhereClause.Remove(WhereClause.Length - 5);

    var query = db.Communities
                .Where(WhereClause)
                .OrderBy("Name");

您可以在此处获取动态 linq 类 http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

祝你好运!

Steven is right, that is one good and easy way to do it and would work great for your needs. If you ever get into a situation where you may have several conditional statements, it would be wise to look into dynamic linq. Dynamic linq would allow you to write a query such as:

    DatabaseDataContext db = new DatabaseDataContext();

    string WhereClause = string.Empty;

    if (!string.IsNullOrEmpty(Name))
        WhereClause += "Name.ToLower().Contains(\"" + Name.ToLower() + "\") AND ";

    if (!string.IsNullOrEmpty(State))
        WhereClause += "City.State.Name.ToLower().Contains(\"" + State.ToLower() + "\") AND ";

    if (!string.IsNullOrEmpty(County))
        WhereClause += "City.County.Name.ToLower().Contains(\"" + County.ToLower() + "\") AND ";

    if (!string.IsNullOrEmpty(City))
        WhereClause += "City.Name.ToLower().Contains(\"" + City.ToLower() + "\") AND ";


    if (WhereClause.EndsWith(" AND "))
        WhereClause = WhereClause.Remove(WhereClause.Length - 5);

    var query = db.Communities
                .Where(WhereClause)
                .OrderBy("Name");

You can get the dynamic linq class here http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

Good Luck!

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