如果你的2組字段是在某種集合中,你可能可以使用LINQ來獲得你的優勢。如果他們在某種集合,它允許您通過密鑰來訪問他們,他們都具有相同的鍵,你可以使用這個(準備粘貼到LINQPad):
Dictionary<string,string> fields1 = new Dictionary<string,string>();
Dictionary<string,string> fields2 = new Dictionary<string,string>();
fields1.Add("field1", "this");
fields2.Add("field1", "this");
fields1.Add("field2", "is");
fields2.Add("field2", "");
fields1.Add("field3", "a");
fields2.Add("field3", null);
fields1.Add("field4", "test");
fields2.Add("field4", "test");
var test =
from f1 in fields1
join f2 in fields2
on f1.Key equals f2.Key
select (f1.Value ?? "") == (f2.Value ?? "");
test.Dump();
如果你的套在2個索引收藏領域以相同的順序,你可以使用這樣的事情:
string[] strings1 = { "this", "is", "a", "test" };
string[] strings2 = { "this", "", null, "test" };
var test =
from s1 in strings1.Select((value,index) => new {value, index})
join s2 in strings2.Select((value,index) => new {value, index})
on s1.index equals s2.index
select (s1.value ?? "") == (s2.value ?? "");
test.Dump();
你能不thave「源」返回字符串空呢,還是希望有一個可用的空字符串? – 2009-11-25 15:00:43