LinqToExcel使用DISTINCT关键字时出现错误

本文关键字:错误 关键字 使用 DISTINCT LinqToExcel | 更新日期: 2023-09-27 18:06:33

我正在尝试使用LinqToExcel库从Excel工作表中提取不同的值。

总是出错

"{"查询表达式'(MOD)'中语法错误(缺少运算符)。"}"

我已经尝试了所有方法,但似乎无法从这列中提取出不同的值。

如有任何帮助,不胜感激。

Thanks in Advance,

代码如下:

public class WorksheetRow
{
    public String CPT { get; set; }
    public String MOD { get; set; }
    public String DESCRIPTION { get; set; }
    public double FEE { get; set; }
}
public class FileUtilsController : Controller
{
    //
    // GET: /FileUtils/
    public ActionResult Index()
    {
        return View();
    }
    public ActionResult FileUpload()
    {
            return View();

    }
    // This action handles the form POST and the upload
    [HttpPost]
    public ActionResult FileUpload(HttpPostedFileBase file)
    {
        // Verify that the user selected a file
        if (file != null && file.ContentLength > 0) 
        {
            // extract only the fielname
            var fileName = Path.GetFileName(file.FileName);
            // store the file inside ~/App_Data/uploads folder
            var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
            file.SaveAs(path);
            var excel = new ExcelQueryFactory(path);
            List<String> worksheetNames = excel.GetWorksheetNames().ToList();
            String FirstWorksheet = worksheetNames[0];
            //Query Column Names
            //The GetColumnNames() method can be used to retrieve the list of column names in a worksheet.
            IEnumerable<String> ProcCodeModifiers = (from row in excel.Worksheet<WorksheetRow>(FirstWorksheet)
                                                     where row.MOD != null
                                                     select row.MOD).Distinct();

            ViewData["Modifiers"] = "";
            foreach (String item in ProcCodeModifiers)
            {
                ViewData["Modifiers"] += item + ", ";
            }
            // redirect back to the index action to show the form once again
//                return RedirectToAction("Index", "Home");
        }
        return View();
    }

LinqToExcel使用DISTINCT关键字时出现错误

我必须做更多的研究为什么会发生这个错误,但一个快速的解决方案是在执行Distinct()操作之前将查询转换为列表。

下面是一个代码示例

IEnumerable<String> ProcCodeModifiers = (from row in excel.Worksheet<WorksheetRow>(FirstWorksheet)
                                         where row.MOD != null
                                         select row.MOD).ToList().Distinct();