【问题标题】:Retrieve and cache a result set into my Application检索结果集并将其缓存到我的应用程序中
【发布时间】:2020-02-19 00:49:09
【问题描述】:

我在一个表中有一个byte[] 列,其中存储了指纹数据。我希望只查询一次表中的行并将记录集存储在变量中或代码中的某个位置,这样我就不必每次都查询数据库。该查询将返回数千行。

这将为我获取所有记录:

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

我尝试在 AppData 类中声明一个变量:public List<object> TableData; 然后尝试将变量“表”值存储到其中。

Data.TableData = table;

错误仍然存​​在:

无法将类型'System.Collections.Generic.List<<anonymous type: FingerprintTEST.tblFingerprint a>>' 隐式转换为'System.Collections.Generic.List<object>'

这就是我希望查询从结果返回的行以匹配指纹的方式:

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)
        {...}
    }
}

有什么想法吗?

【问题讨论】:

  • context.tblFingerprints的数据类型是什么?
  • 数据库是为查询而设计的,你遇到过性能问题吗?
  • @MichaelRandall 好吧,随着我的数据库的增长,检索一个指纹并将其与数千条记录进行比较需要更多时间。

标签: c# entity-framework fingerprint digital-persona-sdk


【解决方案1】:

您将这些作为带有select new {a} 的新对象返回。如果context.tblFingerprintsTableData 类型,你只需要select a

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

【讨论】:

    【解决方案2】:
    • 您不需要select new { a }(这是创建一个新的匿名类型,整个记录只有一个成员,这很愚蠢。
      • 您也根本不需要任何 Linq 表达式,只需在 DbSet 上直接使用 ToList()
    • 将结果存储在静态变量中。
    class Something
    {
        private static List<tblFingerprint> _fingerprints;
    
        public void Do()
        {
            DbContext context = ...
    
            if( _fingerprints is null )
            {
                _fingerprints = context.tblFingerprints.ToList();
            }
    
            // do stuff with `_fingerprints`
        }
    }
    

    【讨论】:

      【解决方案3】:

      删除“new {a}”并仅替换为“a”,并告诉 ToList 这是一个对象列表。

      var table = (from a in context.tblFingerprints
                   select a).ToList<object>();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-05
        • 1970-01-01
        • 2011-01-06
        相关资源
        最近更新 更多