C# 为什么对外部数据源的更改不会反映出来,而对内部数据源显示出来?
为什么外部数据源的更改没有反映出来,而内部数据源的更改却显示出来了??请帮忙C# 为什么对外部数据源的更改不会反映出来,而对内部数据源显示出来?,c#,linq,C#,Linq,为什么外部数据源的更改没有反映出来,而内部数据源的更改却显示出来了??请帮忙 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> result
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------------------------------------------");
}
输出:
ac, ad, bc, bd, **aa. ab, ba, bb**
当我在第二次写入之前交换数据源时,我期望所有可能的组合(ac、ad、bc、bd、ca、cb、da、db)。当我在第二次写入之前执行ToList()时,我得到了预期的结果,这是因为Select是惰性的吗?请解释一下
更新
我尝试的是-在ds交换之后向查询表达式添加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);
//查询语法
IEnumerable resultset=来自inputDS1中的res1
从inputDS2中的res2
选择新的ChrPair(res1、res2);
//写入结果集
Console.WriteLine(“\n\n输出列表-->”;
显示列表(结果集);
//互换头寸
//obs:对第一个ds的更改不会反映在结果集中。
char[]temp=inputDS1;
inputDS1=inputDS2;
inputDS2=温度;
结果集=(来自inputDS1中的res1
从inputDS2中的res2
选择新的ChrPair(res1,res2)).ToList();
//再次运行查询
显示列表(结果集);
您得到的是来自inputDS1
的字符与来自inputDS2
的字符的组合。这就是问题所在
IEnumerable<ChrPair> resultset = from res1 in inputDS1
from res2 in inputDS2
select new ChrPair(res1, res2);
不返回列表或
IEnumerable
。它返回一个包含源(inputDS1
)和输入的方法。当您交换这两者时,您会弄乱方法的输入,但是源代码没有改变(它可能是复制的,而不仅仅是引用的)。当您执行.ToList()时代码>激活该方法,因此不会由于随后调用的命令而获得任何意外行为。从我所看到的,该部分
char[] temp = inputDS1;
inputDS1 = inputDS2;
inputDS2 = temp;
交换输入参数,而不是从linq语句得到的结果。我认为,问题是第一个变量(inputDS1)没有包含在任何匿名函数(lambda)中,那么编译器不会为它生成闭包
编译器将查询转换为如下内容:
IEnumerable<ChrPair> resultset =
inputDS1.SelectMany(c => inputDS2.Select(c1 => new ChrPair(c, c1)));
IEnumerable结果集=
inputDS1.SelectMany(c=>inputDS2.Select(c1=>newchrpair(c,c1));
如您所见,inputDS1
不包含在任何匿名(lambda)中。相反,inputDS2
包含在lambda中,然后编译器将为其生成闭包。因此,在第二次执行查询时,您可以访问修改后的闭包inputDS2
有趣的问题:)我本以为这两个数据源都会被捕获。似乎只有第二个是。看来这就是LINQ是如何转变的。若要“修复”它,请在查询的开始处添加来自u中的。。感谢您的超快速响应-leppie。请您在“添加自”中详细说明“添加自”。此外,我刚刚编辑了我的帖子,以显示我必须做些什么才能获得所需的结果。在更新编辑中,您重建了查询,并将其绑定到新值inputDS1
。是的,但我在第二次执行查询之前更改了inputDss。。。并且是预期的(ac,ad,bc,bd,ca,cb,da,db),但是得到了(ac,ad,bc,bd,aa.ab,ba,bb)。所以-想知道背景发生了什么。。。。希望我说的有道理。@Sumithatterjee我现在明白你的问题了,知道你错在哪里了,这与toList无关。您可以删除它,它仍然可以工作(选中)。让我编辑答案。谢谢,这回答了我的问题。@Sumithatterjee很乐意帮助。请您添加更多细节。谢谢,这回答了我的问题。
char[] temp = inputDS1;
inputDS1 = inputDS2;
inputDS2 = temp;
IEnumerable<ChrPair> resultset =
inputDS1.SelectMany(c => inputDS2.Select(c1 => new ChrPair(c, c1)));