Linq not in

  • 248
  • 0
  • 2018-01-24

Linq not in

 


//將dt_03 key 取出放置 list
List<string> l_filter = new List<string>();
foreach (DataRow dr in dt_03.Rows)
{
	string signup_key = dr["signup_key"].ToString();
	l_filter.Add(signup_key);
}

//dt_02 not in l_filter 資料
IEnumerable<DataRow> q4 = 
	from temp_dt in dt_02.AsEnumerable()
	where !(l_filter).Contains(temp_dt.Field<string>("signup_key"))
	//where !(new string[] { "10-003", "10-004", "10-005" }).Contains(temp_dt.Field<string>("signup_key"))
	select temp_dt;

原本錯誤寫法:

filter = "";
signup_key = "";
foreach (DataRow dr in dt_06.Rows)
{
	signup_key = dr["signup_key"].ToString();
	filter += signup_key + ",";
    //filter += "\"" + signup_key + "\"" + ",";
}
filter = filter.TrimEnd(',');//去掉最後逗號

IEnumerable<DataRow> q7 = (from temp_dt in dt_06.AsEnumerable()
						   where !(new string[] { filter }).Contains(temp_dt.Field<string>("signup_key"))
						   select temp_dt);

問題 : 
這樣串出來會變成 "100001,100002,100003",
但應該要是 "100001","100002","100003" 這種格式才對。
所以錯誤。

若是寫成 filter += "\"" + signup_key + "\"" + ","; 去串,
則會變成 ""100001","100002","100003""
系統編譯後會自動在前後加上 " ,這樣也不對。

 

資料來源

https://dotblogs.com.tw/dc690216/2009/09/13/10601