未在 LinqToExcel 查询中应用的谓词
本文关键字:谓词 应用 LinqToExcel 查询 未在 | 更新日期: 2023-09-27 18:33:48
我遇到了LinqToExcel库,并且一直在玩它。我似乎误解了查询的工作原理。我创建了一个包含一些客户信息的简单工作表。使用 LinqPad,我已将查询设置为:
void Main()
{
string path = @"E:'Documents'LINQPad'LINQPad Queries'LinqToExcel'Customers.xlsx";
var excel = new ExcelQueryFactory(path);
// Mappings and Transforms:
excel.AddMapping<Customer>(c => c.Contact, "Contact Person");
excel.AddMapping<Customer>(c => c.CompanyName, "Company Name");
excel.AddMapping<Customer>(c => c.IsPreferred, "Preferred");
excel.AddTransformation<Customer>(c => c.IsPreferred, cellValue => cellValue == "Yes");
// Query:
var preferrdCompanies = from c in excel.Worksheet<Customer>("Customers")
where c.IsPreferred // this has no effect?
orderby c.CompanyName
select new { c.CompanyName, c.Contact, c.IsPreferred };
// Display:
preferrdCompanies.Dump("Preferred Customers:");
}
// Define other methods and classes here
class Customer
{
public string Contact { get; set; }
public string CompanyName { get; set; }
public bool IsPreferred { get; set; }
}
由于某种原因,未应用谓词。从文本"是"转换为真(布尔)正在工作。如果我写:
preferrdCompanies.ToList().Where(c => c.IsPreferred).Dump("Preferred Customers:");
正如您所期望的那样,我得到了过滤列表。我一直在代码中寻找一个简单的错误,但它没有引发任何异常,我找不到任何明显的错误,所以我想我只是不明白查询的功能?
任何答案/解释将不胜感激,谢谢。
您能否尝试在 where 子句中显式设置c.IsPreferred == true
,看看是否可以解决您的问题。
var preferrdCompanies = from c in excel.Worksheet<Customer>("Customers")
where c.IsPreferred == true
orderby c.CompanyName
select new { c.CompanyName, c.Contact, c.IsPreferred };