1

我遇到了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:");

我得到了你所期望的过滤列表。我一直在我的代码中寻找一个简单的错误,但它没有引发异常,我找不到任何明显错误的东西,所以我想我只是不明白查询的功能?

任何答案/解释将不胜感激,谢谢。

4

1 回答 1

0

您可以尝试在 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 };
于 2014-12-09T22:09:49.187 回答