LINQ查询具有多个StartSwith子句?[英] LINQ query with multiple StartsWith clause?

本文是小编为大家收集整理的关于LINQ查询具有多个StartSwith子句?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个LINQ查询,该查询应如下所示,

var query = DataContext.TenantDataServerTables.Where(p => 
             p.Nursing_Home_Section == homeSection &&
             p.Tenant_Kana_Last.ToString().StartsWith(@"ア") ||
             p.Tenant_Kana_Last.ToString().StartsWith(@"イ") ||
             p.Tenant_Kana_Last.ToString().StartsWith(@"ウ") ||
             p.Tenant_Kana_Last.ToString().StartsWith(@"エ") ||
             p.Tenant_Kana_Last.ToString().StartsWith(@"オ"));
        }

有没有办法做这样的事情来简化查询?

char[] array = new char[] { 'ア', 'イ', 'ウ', 'エ', 'オ' };

var query = DataContext.TenantDataServerTables.Where(p => 
                 p.Nursing_Home_Section == homeSection &&
                 p.Tenant_Kana_Last.ToString().StartsWith(array));

这只是一个示例,因为我必须在LINQ查询上检查StartsWith的更多字符.

推荐答案

因为每个字符串不过是一个字符数组,您可以在其上执行任何LINQ.只需使用第一个字符,然后使用 <其他推荐答案

您可以在 Any 谓词,像这样:

var array = new string[] {@"ア", @"イ", @"ウ", @"エ", @"オ"};
var query = DataContext.TenantDataServerTables.Where(p => 
    p.Nursing_Home_Section == homeSection &&
    array.Any(prefix => p.Tenant_Kana_Last.ToString().StartsWith(prefix))
);

其他推荐答案

如果您只想检查炭,可以使用HimBrombeere的方法.如果数组应包含具有多个字符的字符串,请尝试以下操作:

var query = DataContext.TenantDataServerTables.Where(p =>
    p.Nursing_Home_Section == homeSection &&
    array.Any(str => p.Tenant_Kana_Last.ToString().StartsWith(str)));

本文地址:https://www.itbaoku.cn/post/1556756.html

问题描述

I have a LINQ query that works as it is supposed to as follows,

var query = DataContext.TenantDataServerTables.Where(p => 
             p.Nursing_Home_Section == homeSection &&
             p.Tenant_Kana_Last.ToString().StartsWith(@"ア") ||
             p.Tenant_Kana_Last.ToString().StartsWith(@"イ") ||
             p.Tenant_Kana_Last.ToString().StartsWith(@"ウ") ||
             p.Tenant_Kana_Last.ToString().StartsWith(@"エ") ||
             p.Tenant_Kana_Last.ToString().StartsWith(@"オ"));
        }

Is there a way to do something like this to streamline the query?

char[] array = new char[] { 'ア', 'イ', 'ウ', 'エ', 'オ' };

var query = DataContext.TenantDataServerTables.Where(p => 
                 p.Nursing_Home_Section == homeSection &&
                 p.Tenant_Kana_Last.ToString().StartsWith(array));

This is just an example as there are many more characters that I have to check StartsWith on the LINQ query.

推荐答案

As every string is nothing but an array of characters you could do any linq on it. Just use the very first character and check if it part of your array using Contains:

var query = DataContext.TenantDataServerTables.Where(p => 
             p.Nursing_Home_Section == homeSection &&
             array.Contains(p.Tenant_Kana_Last.ToString()[0]));

Alternativly to p.Tenant_Kana_Last.ToString()[0] you can also use p.Tenant_Kana_Last.First() which is a bit easier to read.

其他推荐答案

You can use your array of "starts" in Any predicate, like this:

var array = new string[] {@"ア", @"イ", @"ウ", @"エ", @"オ"};
var query = DataContext.TenantDataServerTables.Where(p => 
    p.Nursing_Home_Section == homeSection &&
    array.Any(prefix => p.Tenant_Kana_Last.ToString().StartsWith(prefix))
);

其他推荐答案

If you want to check for chars only, you can use HimBromBeere's approach. If the array should contain strings with more than one character, try this:

var query = DataContext.TenantDataServerTables.Where(p =>
    p.Nursing_Home_Section == homeSection &&
    array.Any(str => p.Tenant_Kana_Last.ToString().StartsWith(str)));