我可以用Zip
函数连接两个列表的元素;line1从file1到line1与file2,等等。
var res = lines1.Zip(lines2, (x, y) => $"{x} {y}");
在查询表达式中有什么方法可以执行吗?
var res = from e1 in lines1
where !string.IsNullOrWhiteSpace(e1)
from e2 in lines2
where !string.IsNullOrWhiteSpace(e2)
select $"{e1} {e2}";
这将list1中的每一行与list2中的每一行连接起来,这不是我所需要的。
发布于 2021-09-14 04:20:37
不幸的是,查询语法并不支持通过方法语法可用的所有东西(例如Zip()
方法、自写扩展方法或第三方库(如MoreLinq) )。
由于这一限制,用户无法将此功能导入C#。相反,作为语言所有者的微软不得不实现这样一种功能,就像这里所描述的那样。
发布于 2021-09-14 04:45:39
如果你斜视得很厉害,那就很接近了:
IEnumerable<string> filtered1 =
from e1 in lines1
where !string.IsNullOrWhiteSpace(e1)
select e1;
IEnumerable<string> filtered2 =
from e2 in lines2
where !string.IsNullOrWhiteSpace(e2)
select e2;
IEnumerable<string> res =
from x in filtered1.Zip(filtered2, (e1, e2) => (e1, e2))
select $"{x.e1} {x.e2}";
https://stackoverflow.com/questions/69150364
复制相似问题