views:

287

answers:

4

I'm trying to use the DbfDotNet library (http://dbfdotnet.codeplex.com) and replace my current ADO.NET methods of reading a very large dbf file (350.000+ records). I've tried the existing samples to read my file and file their custom Dataview and I'm very impressed with the speed. In my original code I fill a datatable with the content of the dbf file and use a LINQ query to fill my comboboxes.

I've tried rewriting m LINQ query to use the DbfDotNet table object to fill my combobox but I'm still a novice wit LINQ and can't get it to work.

Could anybody help me out?

This is my current code:

var distinctPostalCodes = (from row in this.fileContent.AsEnumerable()
                           select new
                           {
                               code = row.Field(columnNameCode),
                               name = row.Field(columnNameName)
                           }).OrderBy(x => x.code).Distinct();

Where this.fileContent is my DataTable with address data.

A: 
var distinctPostalCodes = (from row in new DataTable().AsEnumerable()
                               select new
                               {
                                   code = row[columnNameCode],
                                   name = row[columnNameName]
                               }).OrderBy(x => x.code).Distinct();

Try this ,it works fine with me , can you tell me the result or the error ?

Amgad Fahmi
The only one different between [] and Field<T>() is that the last one returns stong-typed data
abatishchev
No it will not retrieve strongly type object , select new actually will retrieve anonymous type , but in this case you will have ienumarable and you can bind it directly to the drop down list
Amgad Fahmi
A: 

If the problem is in fields' type try

code = row.Field<int>(columnNameCode),
name = row.Field<string>(columnNameName)
abatishchev
A: 

I'm getting a compile error on AsEnumerable() with my original code: Instance argument: cannot convert from 'DbfDotNet.DbfTable' to 'System.Data.DataTable' 'DbfDotNet.DbfTable' does not contain a definition for 'AsEnumerable' and the best extension method overload 'System.Data.DataTableExtensions.AsEnumerable(System.Data.DataTable)' has some invalid arguments

Tonight I'll try the suggestion with from row in new DataTable().AsEnumerable() but I'm a bit puzzled with that sample. Which data is being used? My this.fileContent is never used?

Paul Meems
A: 

I finally found it:

        DbfDotNet.Linq.DbfTable<DbfDotNet.DbfRecord> dbftable = new DbfDotNet.Linq.DbfTable<DbfDotNet.DbfRecord>(@"ADRESSER.dbf", System.Text.Encoding.ASCII, DbfDotNet.DbfVersion.dBaseIII);
        // Fill grid:
        dbfTableView1.DbfTable = dbftable;

        var distinctPostalCodes = (from row in dbftable.AsEnumerable()
                                   select new
                                   {
                                       code = row.GetField(4),
                                       name = row.GetField(5)
                                   }).OrderBy(x => x.code).Distinct();

        this.comboBox1.DataSource = distinctPostalCodes.ToArray();
        this.comboBox1.DisplayMember = "code";
        this.comboBox1.ValueMember = "code";

Too bad this code is now as slow as before when I used ADO.NET ;(

Paul Meems