Breaking News

c# – Retrieve and cache a result set into my Application-Exceptionshub

Questions:

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 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<>' to 'System.Collections.Generic.List'

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?

How to&Answers:

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();

Answer:

  • 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 _fingerprints;

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

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

        // do stuff with `_fingerprints`
    }
}

Answer:

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();

About rankhawks

Check Also

jQuery offline slideshow on Android-Exceptionshub

(function($){ $.fn.chocolate = function(args) { // Default Options var options = $.extend({ interval : 1000, …

c# – How to handle mocking (Rhino Mock) and unity container in different unit test classes?-Exceptionshub

Questions: I am working on a large desktop application. The application communicates with number of …

javascript – Actualize Database Data with Functions Cloud-Exceptionshub

Questions: I am developing an Android application and I have a problem that I cannot …

valgrind for Android devices (Crystax). configure: error: C compiler cannot create executables-Exceptionshub

Questions: Using and adapting the script(https://gist.github.com/smits/27060b310546ca6f76568f24838118c3) I get and error ” C compiler cannot create …

c# – Save position of Snake and Detect Collison with it self. Whats wrong?-Exceptionshub

Questions: using System; using System.Windows.Forms; using System.Collections.Generic; using System.Drawing; using OpenTK; using OpenTK.Graphics.OpenGL; using OpenTK.Graphics; …

Leave a Reply

Your email address will not be published. Required fields are marked *