为什么外部数据源的更改没有反映,而它们显示在内部数据源中?请帮忙
public static void MyMethod(char[] inputDS1, char[] inputDS2)
{
Console.WriteLine("\n'from' clause - Display all possible combinations of a-b-c-d.");
//query syntax
IEnumerable<ChrPair> resultset = from res1 in inputDS1
from res2 in inputDS2
select new ChrPair(res1, res2);
//Write result-set
Console.WriteLine("\n\nOutput list -->");
displayList(resultset);
//swap positions
//obs: changes to the first ds is not reflected in the resultset.
char[] temp = inputDS1;
inputDS1 = inputDS2;
inputDS2 = temp;
//run query again
displayList(resultset);
Console.WriteLine("\n------------------------------------------");
}
输入:
('a','b'), ('c','d')
输出:
ac, ad, bc, bd, **aa. ab, ba, bb**
当我在第二次写入之前交换数据源时,我期望所有可能的组合(ac、ad、bc、bd、ca、cb、da、db )。当我在第二次写入之前执行 ToList() 时,我得到了预期的结果,是因为 Select 是懒惰的吗?请解释。
更新
我尝试的是 - 在 ds-swap 之后向查询表达式添加一个 ToList() (强制立即执行)。我得到了正确的结果——ac、ad、bc、bd、ca、cb、da、db。这将返回预期的结果。
//query syntax
IEnumerable<ChrPair> resultset = from res1 in inputDS1
from res2 in inputDS2
select new ChrPair(res1, res2);
//Write result-set
Console.WriteLine("\n\nOutput list -->");
displayList(resultset);
//swap positions
//obs: changes to the first ds is not reflected in the resultset.
char[] temp = inputDS1;
inputDS1 = inputDS2;
inputDS2 = temp;
resultset = (from res1 in inputDS1
from res2 in inputDS2
select new ChrPair(res1, res2)).ToList();
//run query again
displayList(resultset);