2016-11-24 36 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)等 –