LinqtoExcel:无法将 where 子句用于 select 行数据

LinqtoExcel: Not able to use where clause to select a row data

我正在为我的自动化项目使用 linqtoexcel。此文件包含不同的站点 URL 及其凭据。如果我只使用一行,我就可以登录到该站点。但是,如果有两行,那么我在使用 where 子句时就会遇到问题。

如果你有什么不明白的地方,请提供帮助。

  1. My excel Sheet

  2. 下面是我的代码。

    string pathfile = @"..\..\Data.xlsx"; string sheetName = "Login"; var excelFile = new ExcelQueryFactory(pathfile); var abc = from a in excelFile.Worksheet(sheetName).AsEnumerable() where Row.Field<String>("ID").Trim() == "2" select a; PropertiesCollection.driver.Manage().Window.Maximize(); foreach (var a in abc) { PropertiesCollection.driver.Navigate().GoToUrl(a["URL"]); } foreach (var a in abc) { objLogin.Login(a["uname"], a["paswd"]); }

我知道我的代码是错误的。我也知道,我在 Field<String>

中有一个错误

请指导在 linqtoexcel 中使用 where 子句的更好方法。

这将根据您的情况帮助特定行。您只需更改 where 子句。

string pathfile = @"..\..\Data.xlsx";
string sheetName = "Login";
var excelFile = new ExcelQueryFactory(pathfile);
var abc = from a in excelFile.Worksheet(sheetName).AsEnumerable() 
          where a["ID"] == "2" 
          select a;
PropertiesCollection.driver.Manage().Window.Maximize();
foreach (var a in abc)
{
PropertiesCollection.driver.Navigate().GoToUrl(a["URL"]);
}
foreach (var a in abc)
{
objLogin.Login(a["uname"], a["paswd"]);
}