簡體   English   中英

查找包含至少一個字符串單詞的記錄

[英]Find records that contains at least one word of a string

我有桌子醫生

Doctors:
------------
ID
FullName

用戶插入了一個字符串,該字符串可能包含許多單詞,例如:“ Leon Simmons”,而我想讓所有名字都帶有“ Leon”或“ Simmons”的醫生使用。 問題是我不知道用戶將插入多少個單詞。 我如何用linq做這樣的事情?

您可以使用SqlMethods.Like()。

用法示例:

var results =
        from d in doctors
        where SqlMethods.Like(u.FullName, "%Leon Simmons%")
        select d;

一種使用linq方法語法的可能解決方案:

    dbContext.Doctors
     .Where(d => d.FullName.Contains("Leon") || d.FullName.Contains("Simmons"))
     .Count();

嘗試:

IQueryable<Doctor> doctorQuery = dbContext.Doctors;
foreach(var word in searchWords)
    doctorQuery = doctorQuery.Where(x => x.FullName.ToLower().Contains(word.ToLower());
return await doctorQuery.ToListAsync();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM