2016-11-24 40 views
0
var db = new MatriModel(); 
string s = txtKeyWord.Text; 
string[] words = s.Split(','); 
int count = words.Length; 

if (count <= 5) 
{ 
    SerachByKeyWordPanel.Visible = true; 
    var KeyWord = db.tblProfiles.Where(x => words.Contains(x.tblCaste.Caste) && words.Contains(x.tblCountry.Country) && words.Contains(x.City) && words.Contains(x.tblOccupation.Occupation) && words.Contains(x.tblMotherTongue.MotherTongue)).Select(x => new 
    { 
     ProfileID = x.ProfileID, 
     ProfileFor = x.tblProfileFor.ProfileFor, 
    }.ToList(); 

通過以上代碼我可以獲取多個關鍵字的詳細信息,但同時我也想通過單個關鍵字獲取記錄。從單個文本框中搜索單個和多個關鍵字

請有人幫助我,在此先感謝

+4

完全不相關你的問題,但......你爲什麼寫這個? _if(計數<= Convert.ToInt16(「5」))_ – Steve

+1

如果代碼已經與多個搜索字作品我期望它也適用於單一的(如果's.Split(「」)'僅返回一個)。如果不是,你必須解釋發生了什麼問題。 –

+0

不是說沒有找到記錄,通過單個關鍵字 –

回答

0

請參閱基於簡化的對象下面的代碼片段:

class MatrixModel { 
    public string ProfileId {get; set;} 
    public string Caste {get; set;} 
    public string Country {get; set;} 
    public string City {get; set;} 
    public string Occupation {get; set;} 
    public string MotherTongue {get; set;} 
} 

public static void Main() 
{ 
    var db = new MatrixModel[]{ 
     new MatrixModel { 
      ProfileId = "1", 
      Caste = "Caste1", 
      Country = "USA", 
      City = "Miami", 
      Occupation = "System administrator", 
      MotherTongue = "English" 
     }, 
     new MatrixModel { 
      ProfileId = "2", 
      Caste = "Caste1", 
      Country = "India", 
      City = "Mumbai", 
      Occupation = "developer", 
      MotherTongue = "English" 
     },  
     new MatrixModel { 
      ProfileId = "3", 
      Caste = "Caste1", 
      Country = "England", 
      City = "London", 
      Occupation = "developer", 
      MotherTongue = "English" 
     },    
    }; 
    string s = "Caste1, England"; 
    string[] words = s.Split(','); 
    int count = words.Length; 

    if (count <= 5) 
    { 
       IEnumerable<MatrixModel> KeyWord = db; 
       foreach(var par in words) 
       { 
        var parTrimmed = par.Trim(); 
        KeyWord = KeyWord 
         .Where(x => x.Caste == parTrimmed 
          || x.Country == parTrimmed 
          || x.City == parTrimmed 
          || x.Occupation == parTrimmed 
          || x.MotherTongue == parTrimmed); 
       } 

       var result = KeyWord.Select(x => new 
        { 
         ProfileID = x.ProfileId, 
        }).ToList(); 

       foreach(var item in result){ 
        Console.WriteLine(item); 
       } 
     } 
} 

結果如下:

{ ProfileID = 3 } 
+0

我建議'||',但沒有被OP告知。 'count <= 5'表明關鍵字應該分別針對每一列 - 因爲還有5列被檢查 - 但可能會少一些,這就是存在問題的地方。如果搜索關鍵字分離液',,,開發商,'我們可以時尚一些黑客... – bixarrio

+0

我覺得OP與'||問題'是,如果他希望所有的「開發商」從「孟買」,並使用關鍵字'「開發商,孟買」'結果將包含不正確的記錄 – bixarrio

+0

爲什麼我們需要創建一個類,已經我們有在tabels所有數據前:(x.tblCaste.Caste)等 –