請參閱基於簡化的對象下面的代碼片段:
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 }
完全不相關你的問題,但......你爲什麼寫這個? _if(計數<= Convert.ToInt16(「5」))_ – Steve
如果代碼已經與多個搜索字作品我期望它也適用於單一的(如果's.Split(「」)'僅返回一個)。如果不是,你必須解釋發生了什麼問題。 –
不是說沒有找到記錄,通過單個關鍵字 –