Linq / Lamba搜索多个字符串

时间:2016-08-02 08:35:06

标签: c# linq search lambda

我在搜索时遇到了一些问题。我试图在名称+姓氏,参考或公司名称上找到客户。我设法让它工作,但订单不是100%。

这是我的原始对象

var q = from row in DataAccess.metadata.db_Customer
        where row.accountID == accountID
        && row.isActive
        orderby row.Name
        select new bl_customerNames
        {
            customerID = row.customerID,
            CustomerName = row.Name + " " + row.LastName,
            Company = row.Company,
            Reference = row.reference,
            Email = row.Email,
            CurrencyCode = row.CurrencyCode,
            isSuspended = row.isSuspended
        };

然后我做了以下过滤器。

if (!string.IsNullOrEmpty(search))
{
    q = q.Where(x => (x.CustomerName.Contains(search) || x.Reference.Contains(search) || x.Company.Contains(search)));
}

虽然这项工作做得不错,但这个列表并没有真正的逻辑意义,因为它不支持从上面的包含开始。

然后我想出了一个排名解决方案,我会将Starts排在高于Contains的位置。

这是我的代码:

var q = from row in DataAccess.metadata.db_Customer
        where row.accountID == accountID
        && row.isActive
        orderby row.Name
        select new bl_customerNames
        {
            customerID = row.customerID,
            CustomerName = row.Name + " " + row.LastName,
            Company = row.Company,
            Reference = row.reference,
            Email = row.Email,
            CurrencyCode = row.CurrencyCode,
            isSuspended = row.isSuspended,
            Rank = ((row.Name + " " + row.LastName).StartsWith(search) || row.reference.StartsWith(search) || row.Company.StartsWith(search)) ? 1 : ((row.Name + " " + row.LastName).Contains(search) || row.reference.Contains(search) || row.Company.Contains(search)) ? 2 : 0
        };

我会像这样过滤:

q = q.Where(r=>r.Rank > 0).OrderBy(r => r.Rank);

但是,如果我搜索引用,它不会返回任何内容。

所以,我的问题是,如果我搜索引用或公司名称,我当前的方法也能解决为什么没有任何回报?在StartsWith List之后,使用StartsWith然后包含并保持某种字母排序的正确方法是什么?

请记住,我仍然是初级开发人员,任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我回到了基础,这就是我想出来的。

           //Get customers that starts with
            var qSW = from row in DataAccess.metadata.db_Customer
                      where row.accountID == accountID
                      && row.isActive                         
                      where (row.Name + " " + row.LastName).StartsWith(search)
                      || row.Company.StartsWith(search)
                      || row.reference.StartsWith(search)
                      select new bl_customerNames
                      {
                          customerID = row.customerID,
                          CustomerName = row.Name + " " + row.LastName,
                          Company = row.Company,
                          Reference = row.reference,
                          Email = row.Email,
                          CurrencyCode = row.CurrencyCode,
                          isSuspended = row.isSuspended

                      };

            //Order
            qSW = qSW.OrderBy(r => r.CustomerName).ThenBy(r => r.Company).ThenBy(r => r.Reference);

            //Get customers that contains
            var qC = from row in DataAccess.metadata.db_Customer
                      where row.accountID == accountID
                      && row.isActive

                      where (row.Name + " " + row.LastName).Contains(search)
                      || row.Company.Contains(search)
                      || row.reference.Contains(search)
                      select new bl_customerNames
                      {
                          customerID = row.customerID,
                          CustomerName = row.Name + " " + row.LastName,
                          Company = row.Company,
                          Reference = row.reference,
                          Email = row.Email,
                          CurrencyCode = row.CurrencyCode,
                          isSuspended = row.isSuspended                            
                      };

            //If search is less then 3 chars limit to 10 to help with performance
            if(search.Count() > 2)
                qC = qC.OrderBy(r => r.CustomerName).ThenBy(r => r.Company).ThenBy(r => r.Reference);
            else
                qC = qC.OrderBy(r => r.CustomerName).ThenBy(r => r.Company).ThenBy(r => r.Reference).Take(10);

            //Concat list leaving out duplicates
            var SList = qSW.Concat(qC);


            // Return List
            return SList.ToList();

似乎正在做这项工作,而且表现似乎非常好。