LinqToExcel加载字典

时间:2018-06-16 04:40:38

标签: c# performance linq dictionary linq-to-excel

enter image description here我有以下方法,但不太优雅。如果可能的话,我想用ToDictionary来解决这个问题。感谢您提供任何帮助,因为我很新。

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");
    }
}

programs.Dump("Dict");

这就像我得到的那样接近,但不对。错误:&#34;需要IEnumberable字符串类型的接收器,它不能与我一起计算:

var excel = new ExcelQueryFactory(@"E:\MAHipotCepaStationProgram.xlsx");

Dictionary<string, string> programsDict = excel.Worksheet().ToDictionary<string, string>(
                                        e => e["Program #"].Value.ToString() == "Program Title")
                                            .Select(s => s[1].Value.ToString()),
                                        f => f.Where(d => d.Value.ToString() == "Program #").ToString());

2 个答案:

答案 0 :(得分:0)

您可以使用sigle LINQ查询过滤值。这将返回excel中的名称和数字列:

    var sampleExcel = new ExcelQueryFactory(@"I:\Book1.xlsx");
    var sampleWorksheet = from workSheet in sampleExcel.Worksheet("Sheet1") select workSheet;
    var selectedValues = from excelRow in sampleExcel.Worksheet()
       select new { name = excelRow[0], number =Convert.ToInt32(excelRow[1]) };
        foreach (var item in selectedValues)
        {
            Console.WriteLine(string.Format("Name is {0} ,number is {1}",item.name,item.number));
        }


        Dictionary<int, string> dict = new Dictionary<int, string>();

        foreach (var item in selectedValues)
        {
            dict.Add(item.number, item.name);
            Console.WriteLine(string.Format("Name is {0} ,number is {1}", item.name, item.number));
        }

上述LINQ查询的等效lambda表达式:

var selectedValues1 = sampleExcel.Worksheet().Select(x => new { name = x[0], number = x[1] }); 

答案 1 :(得分:0)

快来看看:

do.call(rbind, lapply(2:4, function(x) {
    do.call(rbind, combn(4, x, function(y) {
        data.frame(comb. = paste(y, collapse = ""),
                   Var_comb = paste(c("", y), collapse = "x"),
                   index = sum(G[y,y] %*% B[y] * A[y]),
                   stringsAsFactors = FALSE)
    }, simplify = FALSE))
}))

   comb. Var_comb      index
1     12     x1x2   7.438186
2     13     x1x3  83.270451
3     14     x1x4  37.435149
4     23     x2x3  68.331049
5     24     x2x4  21.273907
6     34     x3x4 145.311921
7    123   x1x2x3  87.071171
8    124   x1x2x4  39.689388
9    134   x1x3x4 174.958491
10   234   x2x3x4 150.810089
11  1234 x1x2x3x4 180.665871

你只需要NuGet&#34; System.Interactive&#34;获得Dictionary<int, string> programsDict = excel .Worksheet() .Select(x => new { A = x[0].ToString(), B = x[1].ToString() }) .ToArray() .Where(x => new [] { "Program #", "Program Title" }.Contains(x.A)) .Buffer(2) .Select(x => new { title = x[0].B, number = int.Parse(x[1].B) }) .ToDictionary(x => x.number, x => x.title); 运营商。

或者使用此实现:

.Buffer(int)