2014-12-09 4 views
1

나는 LinqToExcel 라이브러리를 발견했으며 그걸 가지고 놀았습니다. 쿼리하는 방법을 잘못 이해하고있는 것처럼 보입니다. 일부 고객 정보가 담긴 간단한 워크 시트를 만들었습니다. LinqPad를 사용하여 쿼리를 다음과 같이 설정했습니다.LinqToExcel 쿼리에서 조건부가 적용되지 않음

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

어떤 이유로 든 해당 조건이 적용되지 않습니다. 텍스트 "예"에서 사실 (bool)로 변환 중입니다. 작성한 경우 :

preferrdCompanies.ToList().Where(c => c.IsPreferred).Dump("Preferred Customers:"); 

필자는 예상대로 필터링 된 목록을 얻습니다. 내 코드에서 간단한 오류를 찾고 있었지만 예외가 발생하지 않았고 분명히 잘못된 것을 찾을 수 없기 때문에 쿼리 기능이 어떻게 작동하는지 이해할 수 없습니까?

답변/설명 감사드립니다.

+0

'Where'가 작동하기 전에 ToList를 추가하면 LinqToExcel의 버그 일 가능성이 큽니다. –

+0

그럴 수 있습니다; ToList()를 추가하여 결과 집합을 ExcelQueryable <>에서 List <>로 변경하여 필터링 된 결과를 얻었습니다. 그것은 ExcelQueryable이 술어를 받아들이는 방법을 가지고 있지만 아무 것도하지 않는 것처럼 행동합니다. – Sleette

+0

가능; 또는 부울 열을 제대로 처리하지 않는 버그가 구문 분석에 있습니다. –

답변

0

c.IsPreferred == true을 where 절에 명시 적으로 설정하여 문제가 해결되는지 확인할 수 있습니까?

var preferrdCompanies = from c in excel.Worksheet<Customer>("Customers") 
         where c.IsPreferred == true 
         orderby c.CompanyName 
         select new { c.CompanyName, c.Contact, c.IsPreferred }; 
+0

이상하게도 c.IsPreferred == true (또는 false)를 추가하면 항목이없는 모음 만 반환됩니다. – Sleette

관련 문제