paulyoder / LinqToExcel

Use LINQ to retrieve data from spreadsheets and csv files
MIT License
1.06k stars 299 forks source link

Mapping doesn't work #42

Open bzaar opened 10 years ago

bzaar commented 10 years ago

LinqToExcel seems like such a nice idea but it didn't work for me. Sample project: http://morpher.ru/DictionaryCompiler.zip

I am able to get the row value using row [0] but not using a strongly typed mapping. Why?

paulyoder commented 10 years ago

Sorry @bzaar, I don't have time to unzip your solution file. Can you provide a little more context and share the exact lines of code you're using.

bzaar commented 10 years ago

using System;

namespace DictionaryCompiler { public class Row { public string HeadWord; }

class Program
{
    static void Main()
    {
        var xl = new LinqToExcel.ExcelQueryFactory {FileName = @"..\..\Test.xlsx"};

        foreach (var row in xl.Worksheet ())
        {
            Console.WriteLine (row ["HeadWord"]);
        }

        xl.AddMapping <Row> (r => r.HeadWord, "HeadWord");

        foreach (var row in xl.Worksheet <Row> ())
        {
            Console.WriteLine (row.HeadWord ?? "NULL");
        }
    }
}

}

Output:

some test data

NULL NULL NULL NULL NULL Press any key to continue . . .

Expected output:

some test data

some test data NULL NULL Press any key to continue . . .

Hope this helps.

isaeed commented 10 years ago

yes having the same issue but found that mapping is not working for xlsx files and works in xls. but even in xls, mapping is not accurate. for mapped fields in xls files, data is not fetched and mapped fields are not working in where clause as well. please verify and advise any solution. thanks

eldhoabe commented 10 years ago

hi isaeed , there might be have some accuracy when having multiple where clause , it better to get the values in memory and include your filtration in memory. i have been used linqtoexcel so much yet i dont have any problem with mapping, if your header name is mapped to the correctly i think u can fetch data.

if your issue persis upload ur excel together .

// i have simple where condition in my xlsx file its working good. if u have filtering try in memory. var faceMechResult = excel.WorksheetRange("A5", "AS" + _faceMechRows.ToString(), facemechSheetName). Where(i => i.WorkOrder != null).Select(x => x).ToList();

isaeed commented 10 years ago

hi eldhoabe , thanks for your suggestion about in memory filtration and things are much improved with it.