「其中」條款我有搜索特定字符串的數據表,並返回基於特定條件(唯一ID字段)的整個數據表的子集的代碼。以下示例代碼在數據表中搜索「First」和「Employee」,並僅返回具有相同l_id值的條目。動態構建Linq中
//Get all Id's that satisfy all conditions:
List<int> results = dtResult.AsEnumerable()
// Get all Id's:
.Select(dataRow => dataRow.Field<int>("l_id"))
// Filter the Id's :
.Where(id =>
// the Id should be greater than one.
id > 1
// and the datatable rows with this Id should have a record with W_Text = First
&& dtResult.AsEnumerable().Any(dataRow => dataRow.Field<string>("W_Text") == "First" && dataRow.Field<int>("l_id") == id)
// and the datatable rows with this Id should have a record with W_Text = Employee
&& dtResult.AsEnumerable().Any(dataRow => dataRow.Field<string>("W_Text") == "Employee" && dataRow.Field<int>("l_id") == id))
.Distinct().ToList();
// Get all datatable rows filtered by the list of Id's.
dtCopy = dtResult.AsEnumerable().Where(dataRow => results.Contains((dataRow.Field<int>("l_id")))).CopyToDataTable();
現在,如果我已經動態地獲取搜索文本,那麼該如何處理? 。例如,在上面的代碼中,如果我必須通過「第一」以及「員工」和「薪水」,那麼我該如何繼續?
我怎樣才能讓這個通用?
預先感謝您的幫助!
編輯 - 貌似我沒有在我的崗位明確,所以讓我重新整理再次
我有一個DataTable,它看起來像這樣
l | t | r | b | x | y | w_text | l_id
-------------------------------------------------------------
70 | 314 | 141 | 328 | 1 | 5 | First | 4
149 | 318 | 194 | 328 | 2 | 5 | Employe| 4
204 | 311 | 254 | 326 | 3 | 5 | John | 4
264 | 311 | 325 | 326 | 4 | 5 | Smith | 4
1924 | 310 | 2000 | 329 | 5 | 5 | First | 5
70 | 341 | 109 | 355 | 1 | 6 | step | 5
115 | 340 | 130 | 355 | 2 | 6 | of | 5
136 | 340 | 175 | 355 | 3 | 6 | Linq | 5
185 | 339 | 320 | 356 | 4 | 6 | Last | 6
70 | 394 | 101 | 411 | 1 | 8 | Employe| 6
114 | 390 | 199 | 405 | 2 | 8 | John | 6
210 | 390 | 269 | 405 | 3 | 8 | Doe | 6
在我手上唯一的搜索條件我有是「W_Text'.So我想尋找說獨特的短語‘第一員工’。只有一個l_id(在這種情況下l_id = 4)將有兩個詞‘第一’和‘員工’。如果我搜索分別爲「First」和「Employee」,然後我會得到一個更大的數據集,它不能解決我的目的。我的目標是獲得以下獨特的數據集時,我搜索「的第一名員工」
l | t | r | b | x | y | w_text | l_id
-------------------------------------------------------------
70 | 314 | 141 | 328 | 1 | 5 | First | 4
149 | 318 | 194 | 328 | 2 | 5 | Employe| 4
204 | 311 | 254 | 326 | 3 | 5 | John | 4
264 | 311 | 325 | 326 | 4 | 5 | Smith | 4
在SQL而言,這類似於
Select * From Table where l_id in (Select l_id from Table where W_Text in ('First','Employee') group by l_id having count(l_id) > 1)
我剛纔所說的(由一幫上面的代碼好撒瑪利亞人)完美地工作,並返回上述數據集。問題是,它只適用於「第一名員工」。我在搜索時遇到了「Linq的第一步」。搜索短語在運行時傳遞給程序,可以是多少個單詞。我試圖分離Wheres,但'有'的條件未命中,哪個是整個數據集再次返回的地方。
因此,我要求你們都和藹幫助我解決這個問題。我對Linq非常陌生,我正在努力工作。與此同時,任何幫助,我可以得到高度讚賞。謝謝。
---編輯得到這個工作使用此代碼(與某人的幫助下)
List<string> wTextFilter = new List<string>();
foreach (string sf in strInputString.Split(' ')) //array by splitting on white space
{
wTextFilter.Add(sf);
}
// Get all Id's that satisfy all conditions:
List<int> results = dtResult.AsEnumerable()
// Get all Id's:
.Select(dataRow => dataRow.Field<int>("l_id"))
// Filter the Id's :
.Where(id =>
// the Id should be greater than one.
id > 1 &&
// check if all W_Text entries has a record in the datatable with the same Id.
wTextFilter.All(W_Text => dtResult.AsEnumerable().Any(dataRow => dataRow.Field<string>("W_Text") == W_Text && dataRow.Field<int>("l_id") == id)))
.Distinct().ToList();
// Get all datatable rows filtered by the list of Id's.
dtCopy = dtResult.AsEnumerable().Where(dataRow => results.Contains((dataRow.Field<int>("l_id")))).CopyToDataTable();
使用你自己的邏輯人! –
不太清楚你需要什麼,但是你可以鏈接Where調用,比如'.Where(condition1).Where(condition2)'等等。 – vesan
感謝隊友的建議。我無法破解它,因此來了專家的意見! – DevNovice