2012-11-16 63 views
0

我有一個搜索功能,查找客戶名稱,郵編,電話號碼。我也希望函數查找一個連接的表CustomerContact來搜索FirstName和Surname。.net MVC搜索查詢加入表

我有以下幾點:

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())); 
} 

我沒有得到任何錯誤。當我搜索名字和姓氏時,它不會返回任何結果。

回答

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()); 
     } 
0

客戶= customer.Include(「CustomerContact」)。其中(...

+0

不幸的是,我更新了我的原始問題以顯示完整的查詢。 – Stephen