Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用.Contains()搜索组合两列的参数的查询_C#_Linq_Linq To Sql - Fatal编程技术网

C# 使用.Contains()搜索组合两列的参数的查询

C# 使用.Contains()搜索组合两列的参数的查询,c#,linq,linq-to-sql,C#,Linq,Linq To Sql,我有一个表,其中有FirstName和LastName,还有一个查询,看起来有点像这样: var TheQuery = (from c in MyDC.Contacts where (c.FirstName.Contains(TheSearchParameter) || c.LastName.Contains(TheSearchParameter)) select c.ColumnID

我有一个表,其中有FirstName和LastName,还有一个查询,看起来有点像这样:

var TheQuery = (from c in MyDC.Contacts
                where (c.FirstName.Contains(TheSearchParameter)  ||
                       c.LastName.Contains(TheSearchParameter))
                select c.ColumnID).Distinct().ToList();
string TheSearchParameter = "John Smith";
TheSearchParameter = TheSearchParameter.ToLower(); //case insensitive
string[] pars = TheSearchParameter.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries); //to get all the pars
var TheQuery = (from c in MyDC.Contacts
                let l = new List<string>() {c.FirstName.ToLower(), c.LastName.ToLower()}
                where l.Except(pars).Count() <= l.Count - pars.Length
                select c.ColumnID).Distinct().ToList();

如果用户搜索John或Smith,将返回记录,但如果用户搜索John Smith,则不会返回任何记录。<代码>包含()<代码>工作,在查询中,我需要更改什么以使它正常工作?

< p>您可以考虑在LINQ表达式中添加<代码> Stists/<代码>方法。StartsWith的工作原理类似于SQL
,其中的列类似于'Something%'
。您还可以添加
EndsWith
,以使搜索范围更广

var TheQuery = (from c in MyDC.Contacts
                where (c.FirstName.Contains(TheSearchParameter)  ||
                       c.FirstName.StartsWith(TheSearchParameter) ||
                       c.FirstName.EndsWith(TheSearchParameter) ||
                       c.LastName.StartsWith(TheSearchParameter) ||
                       c.LastName.EndsWith(TheSearchParameter) ||
                       c.LastName.Contains(TheSearchParameter))
                select c.ColumnID).Distinct().ToList();
使用
(条件&&condition)|
将强制括号的内部成为单个表达式,同时保留现有条件


如果没有,请告诉我。希望这能有所帮助。

我不太明白你为什么希望你的陈述能奏效

string x =  "abc" ;
string y = "abc def";

y.Contains(x); // returns true
x.Contains(y); // returns false

请注意我的方法

如果搜索全名,上面的两个答案都不会返回,因为它们只使用FirstName和LastName进行搜索

(例如:

<> >第一部分,其中包含(C.Fult/LaSTNEX)< /C> >考虑第一部分和最后部分名称< /P>查找<代码>参数> <代码> > 第二部分将考虑用“代码>搜索参数< <代码> < /P>查找姓氏或姓氏。 如果你搜索“约翰·史密斯”

还有约翰·史密斯、约翰·保罗和保罗·史密斯的唱片

第一部分将返回1个结果:John Smith 第二部分将返回0

如果你搜索“约翰”

第一部分将返回0结果 第二部分将返回:约翰·史密斯和约翰·保罗

如果你搜索“史密斯”

第一部分将返回0
第二部分将返回:John Smith和Paul Smith

我目前的理解是,您希望跨列搜索单词。虽然我确信我还没有完全理解这些要求,但我想提出以下想法。希望您能够将其变成一个完整的解决方案:

var words = searchQuery.Split(' ');
var query = (from c in MyDC.Contacts
             select c);

//Force each word to occur.
foreach (var w in words)
 query = query.Where(c => c.FN.Contains(w) || c.LN.Contains(w));

var TheQuery = (from c in query
                select c.ColumnID).Distinct().ToList();
这引入了搜索所有单词的概念,而不是只搜索一个字符串


如果你想要名字和姓氏的独立逻辑,你可以有两个单词列表:一个是FN,一个是LN。

而不是开始玩
开始与
结束与
的组合,让我们分析一下主要问题:

使用
.Contains()
搜索组合两列的参数

因此,一般的答案是:

where Combine(table.Column1, table.Column2).Contains(TheSearchParameter)
问题是什么是
Combine
函数,对此没有一般性的答案

在您的特定情况下,似乎要搜索一个
名称
,它是
FirstName
LastName
列的组合

即使这种组合通常也没有定义(不同的语言有不同的名称规则),但假设您想到了最常见的名称组合器:

Name = "{FirstName} {LastName}"
那么查询就简单了

var TheQuery = (from c in MyDC.Contacts
                where (c.FirstName + " " + c.LastName).Contains(TheSearchParameter)
                select c.ColumnID).Distinct().ToList();

例如,如果有名为“约翰”、“史密斯”的联系人,则上述查询将匹配“约翰”、“史密斯”、“约翰·史密斯”,而不是“史密斯·约翰”.

正如您所说,让我们假设集合包含一条名为
John
且LastName为
Smith
的记录,还有另一条名为“Sam”且LastName为“SmithX”的记录。
TheSearchParameter
将包含要搜索的单词

现在,让我们看看您的查询将如何处理不同的输入。

案例1:TheSearchParameter='John'-将为您提供预期结果,因为
c.FirstName.Contains(TheSearchParameter)
的计算结果为
true

案例2:TheSearchParameter='Smith'-将为您提供预期结果,因为
c.LastName.Contains(TheSearchParameter)
的计算结果为
true

案例3:*搜索参数='Jo'*将为您提供预期结果,因为
c.FirstName.Contains(搜索参数)
的计算结果为
true

到目前为止,一切都很好;现在

案例4:TheSearchParameter='John Smith'-将为您提供空结果集,因为没有包含
John Smith
的名字或姓氏,但它是两者的组合

在所有这些情况下,您可以采取哪些措施来获得正确的结果:

由于搜索参数
可能包括firstName或LastName或两者,请在搜索参数
中执行搜索操作。因此,您的查询可能如下所示:

var TheQuery = (from c in MyDC.Contacts
                where (TheSearchParameter.Contains(c.FirstName)  ||
                       TheSearchParameter.Contains(c.LastName))
                select c.ColumnID).Distinct().ToList();
现在,对于案例1-3,它给出了相同的结果,并将给出两条记录(“John Smith”和“Sam Smith”,尽管姓氏不同。因为
TheSearchParameter.Contains(c.LastName)
的计算结果为true;现在我们必须对其进行排序

所以事实是

如果输入是FirstName和LastName的组合,我们必须确认组合应以
FirstName
开头,并以
LastName
结尾。因此,我建议您使用这种搜索机制。以下代码片段将帮助您做到这一点

  var TheQuery = (from c in MyDC.Contacts
                  where ((TheSearchParameter.Contains(c.FirstName) && TheSearchParameter.StartsWith(c.FirstName)) ||
                        (TheSearchParameter.Contains(c.LastName) && TheSearchParameter.EndsWith(c.FirstName)))
                 select c.ColumnID).Distinct().ToList();
  • 如果我们假设名字是由名字和姓氏组成的 仅,并且您的数据中没有中间名。用户全名= 名字+姓氏。您可以尝试以下代码:

    var TheQuery = (from c in MyDC.Contacts
                where (c.FirstName + " " + c.LastName).Contains(TheSearchParameter)
                select c.ColumnID).Distinct().ToList();
    
  • 我们在搜索参数中检查
    FirstName
    LastName

    var TheQuery = (from c in MyDC.Contacts
                where (c.FirstName.Contains(TheSearchParameter) ||
                        c.LastName.Contains(TheSearchParameter) ||
                        TheSearchParameter.StartsWith(c.FirstName) ||
                        TheSearchParameter.EndsWith(c.LastName)
                        )
                select c.ColumnID).Distinct().ToList();
    
    e、 例如:Jhon Smith
    “Jhon Smith”。以(“Jhon”)
    “Jhon”开头
    当Jhon或Smith在时,Smith“.EndsWith(“Smith”)
    为真
    联系人


  • 我认为每个人都在把简单的事情复杂化。主要的意图是得到与给定名称匹配的结果联系。有两种
    var TheQuery = (from c in MyDC.Contacts
                where (c.FirstName.Contains(TheSearchParameter) ||
                        c.LastName.Contains(TheSearchParameter) ||
                        TheSearchParameter.StartsWith(c.FirstName) ||
                        TheSearchParameter.EndsWith(c.LastName)
                        )
                select c.ColumnID).Distinct().ToList();
    
        var TheQuery = (from c in MyDC.Contacts
                        where (TheSearchParameter.Split(' ').Any(s => c.FirstName.Contains(s)) ||
                        TheSearchParameter.Split(' ').Any(s => c.LastName.Contains(s)))
                        select c.ColumnID).Distinct().ToList();
    
    var TheQuery = (from c in MyDC.Contacts
          where (c.FirstName + " " + c.LastName == TheSearchParameter)
          select c.ColumnID).Distinct().ToList();
    
            string[] parametersArray = null;
            int parametersCount = 0;
            if(!string.IsNullOrEmpty(TheSearchParameter))
            {
                parametersArray = TheSearchParameter.Split(new char[0],StringSplitOptions.RemoveEmptyEntries);
                parametersCount = parametersArray.Length;
            }
    
            var TheQuery = (from c in MyDC.Contacts
                            where (parametersCount == 0 || (parametersCount > 0 && (parametersArray.Contains(c.FirstName) ||
                                   parametersArray.Contains(c.LastName))))
                            select c.ColumnID).Distinct().ToList();
    
    var results = MyDC.Contacts
      .Where(c => string.Join(" ",c.FirstName,c.LastName).Contains(TheSearchParameter))
      .Select(c => c.ColumnId)
      .Distinct()
      .ToList();
    
    John
    vbJohn
    John123
    vbJohn123
    
    john
    vbjohn
    john123
    vbjohn123
    
    John Bryant
    John Bard
    Smith Bard
    John Smith
    Ralla Smith
    Smith John
    Metajohn Rasmith
    
    John Bryant
    John Bard
    John Smith
    Smith John
    
    Smith Bard
    John Smith
    Ralla Smith
    Smith John
    
    John Smith
    Smith John
    
    string TheSearchParameter = "John Smith";
    TheSearchParameter = TheSearchParameter.ToLower(); //case insensitive
    string[] pars = TheSearchParameter.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries); //to get all the pars
    var TheQuery = (from c in MyDC.Contacts
                    let l = new List<string>() {c.FirstName.ToLower(), c.LastName.ToLower()}
                    where l.Except(pars).Count() <= l.Count - pars.Length
                    select c.ColumnID).Distinct().ToList();
    
    where l.Except(pars).Count() <= l.Count - pars.Length
    
    var TheQuery = (from c in MyDC.Contacts
                    where (System.Threading.Thread.CurrentThread.CurrentCulture.CompareInfo.IndexOf(c.FirstName, TheSearchParameter, System.Globalization.CompareOptions.IgnoreCase) >= 0 ||
                           System.Threading.Thread.CurrentThread.CurrentCulture.CompareInfo.IndexOf(c.Lastname, TheSearchParameter, System.Globalization.CompareOptions.IgnoreCase) >= 0)
                           select c.ColumnID).Distinct().ToList();