LINQ to SQL 查询帮助(字符串包含字符串数组中的任何字符串)

发布于 2024-09-18 09:29:25 字数 581 浏览 3 评论 0原文

我一直在用这个撕掉我的头发。我有一个搜索词数组,我正在尝试执行 LINQ to SQL 查询来针对数组中的每个项目搜索字段值。

我已经走到这一步了

var searchResults = 
    from x in SDC.Staff_Persons
    where staffTermArray.Any(pinq => x.Forename.Contains(pinq))
        || staffTermArray.Any(pinq => x.Surname.Contains(pinq))
        || staffTermArray.Any(pinq => x.Known_as.Contains(pinq))
    orderby x.Surname
    select x;

......但后来得到了

本地序列不能在 LINQ 中使用 到SQL执行查询 除 Contains() 之外的运算符 运算符

...现在我被困住了。

如果有人能提供帮助,我将非常感激。 提前致谢。

I've been tearing my hair out with this one. I've got an array of search terms and I'm trying to do a LINQ to SQL query to search field values against each item in the array.

I got this far..

var searchResults = 
    from x in SDC.Staff_Persons
    where staffTermArray.Any(pinq => x.Forename.Contains(pinq))
        || staffTermArray.Any(pinq => x.Surname.Contains(pinq))
        || staffTermArray.Any(pinq => x.Known_as.Contains(pinq))
    orderby x.Surname
    select x;

... but then got

Local sequence cannot be used in LINQ
to SQL implementation of query
operators except the Contains()
operator

... and now I'm stuck.

If anyone can help I'd be very grateful.
Thanks in advance.

Rob

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

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

发布评论

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

评论(2

昨迟人 2024-09-25 09:29:25

我不确定这是否是最简单的解决方案,但这会起作用:

var filter = CreateFilter(staffTermArray);

var searchResults = 
    from person in SDC.Staff_Persons.Where(filter)
    orderby person.Surname
    select person;



private static Expression<Func<Staff_Person, bool>> CreateFilter(
    string[] staffTermArray)
{
    var predicate = PredicateBuilder.False<Staff_Person>();

    foreach (var staffTerm in staffTermArray)
    {
       // We need to make a local copy because of C# weirdness.
       var ping = staffTerm;

       predicate = predicate.Or(p => p.Forename.Contains(ping));
       predicate = predicate.Or(p => p.Surname.Contains(ping));
       predicate = predicate.Or(p => p.Known_as.Contains(ping));
    }

    return predicate;
}

您将需要 PredicateBuilder 为此。

I'm not sure if this is the easiest solution, but this will work:

var filter = CreateFilter(staffTermArray);

var searchResults = 
    from person in SDC.Staff_Persons.Where(filter)
    orderby person.Surname
    select person;



private static Expression<Func<Staff_Person, bool>> CreateFilter(
    string[] staffTermArray)
{
    var predicate = PredicateBuilder.False<Staff_Person>();

    foreach (var staffTerm in staffTermArray)
    {
       // We need to make a local copy because of C# weirdness.
       var ping = staffTerm;

       predicate = predicate.Or(p => p.Forename.Contains(ping));
       predicate = predicate.Or(p => p.Surname.Contains(ping));
       predicate = predicate.Or(p => p.Known_as.Contains(ping));
    }

    return predicate;
}

You will need the PredicateBuilder for this to work.

很酷又爱笑 2024-09-25 09:29:25

一种选择是在客户端而不是在 SQL 中进行过滤。您可以通过调用 AsEnumerable() 强制在客户端上计算 where。但是,这意味着表的每一行都会在测试匹配之前加载到内存中,因此如果您的搜索仅匹配大型表中的少量结果,则效率可能会低得令人无法接受。

var allPersons = 
    from x in SDC.Staff_Persons
    orderby x.Surname
    select x;

var searchResults = 
    from x in allPersons.AsEnumerable()
    where staffTermArray.Any(pinq => x.Forename.Contains(pinq))
        || staffTermArray.Any(pinq => x.Surname.Contains(pinq))
        || staffTermArray.Any(pinq => x.Known_as.Contains(pinq))
    select x;

One option would be to do the filtering on the client rather than in SQL. You can force the where to be evaluated on the client by calling AsEnumerable(). However, this means that every row of the table is loaded into memory before being tested for a match, so it may be unacceptably inefficient if your search matches only a small number of results from a large table.

var allPersons = 
    from x in SDC.Staff_Persons
    orderby x.Surname
    select x;

var searchResults = 
    from x in allPersons.AsEnumerable()
    where staffTermArray.Any(pinq => x.Forename.Contains(pinq))
        || staffTermArray.Any(pinq => x.Surname.Contains(pinq))
        || staffTermArray.Any(pinq => x.Known_as.Contains(pinq))
    select x;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文