📜  如何创建更准确的搜索c#代码示例

📅  最后修改于: 2022-03-11 14:49:03.834000             🧑  作者: Mango

代码示例1
var keywords = new [] {"Sergey", "Berezovskiy"};

var users = from u in context.Users
            let match = (keywords.Contains(u.FirstName) ? 1 : 0) +
                        (keywords.Contains(u.LastName) ? 1 : 0) +
                        (keywords.Contains(u.MiddleName) ? 1 : 0)
            where match > 0
            orderby match descending, 
                    u.LastName, u.FirstName
            select u;

var methodChain = context.Users
                .Select(u => new
                {
                    u,
                    match = (keywords.Contains(u.FirstName) ? 1 : 0) +
                            (keywords.Contains(u.LastName) ? 1 : 0) +
                            (keywords.Contains(u.MiddleName) ? 1 : 0)
                })
                .Where(t => t.match > 0)
                .OrderByDescending(t => t.match)
                  .ThenBy(t.u.LastName)
                  .ThenBy(t.u.FirstName)
                .Select(t => t.p);