【发布时间】:2018-06-16 14:16:42
【问题描述】:
我无法将这些表达式转换为 lambdas 而没有错误(无法转换类型错误):
var excel = new ExcelQueryFactory(@"E:\MAHipotCepaStationProgram.xlsx");
//get list of program names
List<string> testNames = new List<string>();
testNames.AddRange(excel.Worksheet().ToList()
.Where(s => s["Program #"].Value.ToString() == "Program Title")
.Select(s => s[1].Value.ToString()));
//get list of program numbers
List<int> testNumbers = new List<int>();
testNumbers.AddRange(excel.Worksheet().ToList()
.Where(s => s["Program #"].Value.ToString() == "Program #")
.Select(s => Convert.ToInt32(s[1].Value)));
//combine them
Dictionary<int, string> programs = new Dictionary<int, string>();
for (int x = 0; x < testNames.Count-1; x++)
{
if (!programs.ContainsKey(Convert.ToInt32(testNumbers[x])))
{
programs.Add(Convert.ToInt32(testNumbers[x]), testNames[x]);
}
else
{
testNumbers[x].Dump("Duplicate Found");
}
}
我们应该查看类似以下的内容,但无论我尝试什么,它都无法编译:
Dictionary<string, string> programsDict = excel.Worksheet().ToDictionary(
e => e["Program #"].Value.ToString() == "Program Title")
.Select(s => s[1].Value.ToString()),
f => f.Where(d => d.Value.ToString() == "Program #").ToString());
非常感谢任何帮助。另外,这里是我们试图从中提取数据的 Excel 表的图片,即节目标题和节目编号。
【问题讨论】:
-
看看 LINQ 中的
Zip方法。如果您添加 Excel 工作表外观的图像,可能有更好的方法,您可以在一个步骤中获取两个值,而不是使用匿名类型或元组在ToDictionary调用之前捕获这两个值。跨度> -
@Ian Mercer - 好的,电子表格已添加。真的很好奇 Zip 如何在这里与 ToDictionary 调用一起工作......
-
错误是什么?
-
"Cannot apply indexing to the Cell"是编译的错误
-
@ian mercer var zip = testNames.Zip(testNumbers, (code, state) => code + ": " + state);给我一个 IEnumberable 字符串,我将如何获得 IEnumberable 字符串?
标签: c# linq lambda extension-methods linq-to-excel