0

I have a byte[] column in a table that has fingerprint data stored in it. I wish to query the rows from a table only once and store the record set in a variable or somewhere in my code, so that I don't have to query the database each and every time. The query will return thousands of rows.

This will fetch all the records for me:

var table = (from a in context.tblFingerprints
                              select new {a} ).ToList();

I tried declaring a variable in AppData class: public List<object> TableData; Then tried to store the variable 'table' values to it.

Data.TableData = table;

The error remains:

Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type: FingerprintTEST.tblFingerprint a>>' to 'System.Collections.Generic.List<object>'

This is how I wish to query the rows returned from the result for matching fingerprint:

foreach (var row in Data.TableData)
{
    Template tem = new Template();
    tem.DeSerialize(row.a.fingerTemplate);

    if (tem != null)
    {
        // Compare feature set with particular template.
        Verificator.Verify(features, tem, ref res);

        if (res.Verified)
        {...}
    }
}

Any ideas please?

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
DevRingim
  • 9
  • 5

3 Answers3

0

You are returning these as new objects with select new {a}. If context.tblFingerprints is of type TableData you just need to select a

var table = (from a in context.tblFingerprints
                          select a).ToList();
chrisbyte
  • 1,408
  • 3
  • 11
  • 18
0
  • You don't need the select new { a } (this is creating a new anonymous-type, with a single member for a whole record, which is silly.
    • You also don't need any Linq expression at all, just use ToList() on the DbSet directly.
  • Store the result in a static variable.
class Something
{
    private static List<tblFingerprint> _fingerprints;

    public void Do()
    {
        DbContext context = ...

        if( _fingerprints is null )
        {
            _fingerprints = context.tblFingerprints.ToList();
        }

        // do stuff with `_fingerprints`
    }
}
Dai
  • 141,631
  • 28
  • 261
  • 374
0

Remove the "new {a}" and replace with just "a", and tell ToList that it's a list of objects.

var table = (from a in context.tblFingerprints
             select a).ToList<object>();
BrianM
  • 836
  • 9
  • 17