我有一个搜索功能,可以查找客户名称,邮政编码,电话号码。我还希望该函数能够查找连接表CustomerContact以搜索FirstName和Surname。
我有以下内容:
var customer = from c in db.Customer.Include(c => c.CustomerContact)
select c;
if (!String.IsNullOrEmpty(searchString))
{
customer = customer.Where(c => c.CustomerName.ToUpper().Contains(searchString.ToUpper())
|| c.Postcode.ToUpper().Contains(searchString.ToUpper())
|| c.CustomerContact.FirstName.ToUpper().Contains(searchString.ToUpper())
|| c.CustomerContact.Surname.ToUpper().Contains(searchString.ToUpper())
|| c.Telephone.ToUpper().Contains(searchString.ToUpper()));
}
我没有收到任何错误。当我搜索FirstName和Surname时,它不会返回任何结果。
答案 0 :(得分:0)
customer = customer.Include(“CustomerContact”)。Where(...
答案 1 :(得分:0)
在处理其他事情时遇到了这个答案。
这是我的最终代码。希望它对某人有用。
var customer = from c in db.Customer
select c;
if (!String.IsNullOrEmpty(searchString))
{
customer = customer.Where(c => SqlFunctions.StringConvert((double)c.CustomerID).Trim().Equals(searchString)
|| c.CustomerName.ToUpper().Contains(searchString.ToUpper())
|| c.Postcode.ToUpper().Replace(" ", "").Equals(searchString.ToUpper().Replace(" ", ""))
|| c.Telephone.ToUpper().Replace(" ", "").Equals(searchString.ToUpper().Replace(" ", ""))
|| c.CustomerContact.Where(x => x.FirstName.ToUpper().Contains(searchString.ToUpper())).Any()
|| c.CustomerContact.Where(x => x.Surname.ToUpper().Contains(searchString.ToUpper())).Any()
|| c.CustomerContact.Where(x => (x.FirstName.ToUpper() + " " + x.Surname.ToUpper()).Contains(searchString.ToUpper())).Any());
}