用户搜索屏幕的 Linq 动态查询

发布于 2024-10-30 04:57:33 字数 772 浏览 0 评论 0原文

我有一个数据库,它有一个“动态”的用户搜索屏幕,因为我可以根据搜索所基于的特定视图中可用的列动态添加其他搜索条件,并且它将允许用户使用它们立即地。以前我一直在该数据库中使用 nettiers,但现在我正在使用 RIA、EntFramework 4 和 LINQ 针对它编写一个新应用程序。

我目前有 2 个表用于此目的,其中一个用可用的搜索字符串模式填充组合框:

LastName 姓氏、名字 电话 等等......

然后我有另一个表将这些标准分开并在我的 nettiers 算法中使用。它工作得很好,但我想使用 LINQ..但它不太适合这个模型。此外,我认为我可以使用 linq 将其缩减为一张表...

使用类似于此的格式或非常接近的格式...

ID CriteriaWhereClause 1 LastName 'Lastname Like '%{0}%'

现在我知道这不适合 linq 查询。但为了清楚起见,我尝试使用通用语法...

真正的 where 子句看起来像这样: a=>a.LastName.Contains("{0}")

我的第一个问题是:这可能吗?将 lambda 输入字符串并在 Linq 查询中使用它?

我的第二个问题是:在我研究这个问题之前,我发现了一种具有类似 it.LastName{0} 前缀的 linq 语法。 我似乎尝试过使用它,因为它的痕迹仍然在我的测试数据库中......但我不记得我在哪里读到过它。

有人这样做吗?我做了一些搜索并发现了类似的情况,但它们大多具有可选的静态字段,这与我正在做的方式不完全一样......

I have a database that has a user search screen that is "dynamic" in that I can add additional search criteria on the fly based on what columns are available in the particular view the search is based on and it will allow the user to use them immediately. Previously I had been using nettiers for this database, but now I am programming a new application against it using RIA and EntFramework 4 and LINQ.

I currently have 2 tables that are used for this, one that fills the combobox with the available search string patterns:

LastName
LastName, FirstName
Phone
etc....

then I have an other table that splits those criteria out and is used in my nettiers algorithms. It works well, but I want to use LINQ..and it doesnt fit this model very well. Besides I think I can pare it down to just one table with linq...

using a format similar to this or something very close...

ID Criteria WhereClause
1 LastName 'Lastname Like '%{0}%'

now I know this wont fit specifically into a linq query..but I am trying to use a univeral syntax for clarity here...

the real where clause would look something like this: a=>a.LastName.Contains("{0}")

My first question is: Is that even possible to do? Feed a lambda in to a string and use it in a Linq Query?

My second question is: at one point when I was researching this before I found a linq syntax that had a prefix like it.LastName{0}
and I appear to have tried using it because vestiges of it are still in my test databases...but I dont know recall where I read about it.

Is anyone doing this? I have done some searches and found similar occurances but they mostly have static fields that are optional, not exactly the way I am doing it...

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

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

发布评论

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

评论(2

护你周全 2024-11-06 04:57:33

至于你的第一个问题,你可以使用 Dynamic Linq 来完成此操作,如 Scott Gu 此处

var query = Northwind.Products.Where("Lastname LIKE "test%");

As for your first question, you can do this using Dynamic Linq as described by Scott Gu here

var query = Northwind.Products.Where("Lastname LIKE "test%");
你曾走过我的故事 2024-11-06 04:57:33

我不确定您的动态查询需要多详细,但是当我需要进行动态查询时,我创建一个类来表示过滤器值。然后我将该类传递给存储库中的搜索方法。如果字段的值为空,则查询将忽略它。如果它有一个值,它会添加适当的过滤器。

public class CustomerSearchCriteria{
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string PhoneName { get; set; }
}


public IEnumberable<Customer> Search(CustomerSearchCriteria criteria){
    var q = db.Customers();

    if(criteria.FirstName != null){
        q = q.Where(c=>c.FirstName.Contains(criteria.FirstName));
    }

    if(criteria.LastName!= null){
        q = q.Where(c=>c.LastName.Contains(criteria.LastName));
    }

    if(criteria.Phone!= null){
        q = q.Where(c=>c.Phone.Contains(criteria.Phone));
    }
    return q.AsEnumerable();
}

I'm not sure how detailed your dynamic query needs to be, but when I need to do dynamic queries, I create a class to represent filter values. Then I pass that class to a search method on my repository. If the value for a field is null then the query ignores it. If it has a value it adds the appropriate filter.

public class CustomerSearchCriteria{
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string PhoneName { get; set; }
}


public IEnumberable<Customer> Search(CustomerSearchCriteria criteria){
    var q = db.Customers();

    if(criteria.FirstName != null){
        q = q.Where(c=>c.FirstName.Contains(criteria.FirstName));
    }

    if(criteria.LastName!= null){
        q = q.Where(c=>c.LastName.Contains(criteria.LastName));
    }

    if(criteria.Phone!= null){
        q = q.Where(c=>c.Phone.Contains(criteria.Phone));
    }
    return q.AsEnumerable();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文