落实byte []的LINQ查询我自己的类型 [英] Implement Linq query on byte[] for my own type

查看:301
本文介绍了落实byte []的LINQ查询我自己的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我自己的小数据库,我的工作(学习目的)。数据存储为二进制在磁盘上。 我可以做一个全表扫描和我的数据库将返回每个行作为一个byte [] ROW_BUFFER。 所述ROW_BUFFER包含每一行的字段。从逻辑上讲,它看起来像:

I have my own little database I am working on (for learning purposes). Data is stored as binary on disk. I can do a full table scan and my database will return each "row" as a byte[] row_buffer. The row_buffer contains the fields of each row. Logically, it looks like:

ROW_BUFFER = [[var_string] [INT32] [var_string]

我有一个表定义结构定义的列。点是,我知道每个数据字段的每行中的位置。

I have the column defined in a "Table Definition" structure. Point is, I know the position of each data field in each row.

现在,我想实现的LINQ能力,从我的数据库中查询数据(简单查询):

Now, I want to implement Linq capabilities to query data from my database (simple queries):

var DemoTable = new Table<PeopleClass>();
var result = DemoTable.Where(p => p.Name == "Peter").Select(p => p).ToList();

在我的表类,我已经实现了以下逻辑:

In my Table class, I have implemented the following logic:

public IEnumerable<T> Where(Func<T, bool> predicate)
{
  foreach (var item in this.rows)
  {
    if (predicate(item)) yield return item;
  }
}

注:this.rows是包含数据库的所有记录列表

Note: this.rows is a List that contains all records of the database.

我的问题是,产生PeopleClass对象的每一行是非常低效的(由于转换字节[]字符串)。我想推迟创造尽可能多的。

My issue is that generating PeopleClass objects for each row is very inefficient (due to converting bytes[] to string). I want to defer the creation as much as possible.

目前,我正在研究的条件为byte []和比较,直接agains我行转换。但我到目前为止还没有。

Currently, I am investigating to convert the condition to byte[] and compare that directly agains my rows. But I have failed so far.

我如何实现的IEnumerable&LT; T&GT;凡(Func键&LT; T,布尔&GT; predicate)来对ROW_BUFFER,而不必产生PeopleClass对象第一个直接操作?只有全成命中应转换为PeopleClass对象

How can I implement IEnumerable<T> Where(Func<T, bool> predicate) to directly operate on the row_buffer instead of having to generate the PeopleClass objects first? Only the successfull hits should be converted to PeopleClass objects.

我没有找到一个方法来提取在predicate条件(其中字段名称,例如名称和其值的字段是必需的)。

I did not find a way to extract the condition (which field name e.g. "Name" and which value for the field is required) in the predicate.

任何帮助将是非常美联社preciated。

Any help would be highly appreciated.

推荐答案

首先,它看起来像你需要创建一个LINQ提供程序,则不需要实施其中,等,包括已经完成。我会从那里,看着在任何可能的性能问题之前。

To start with, it looks like you need to create a LINQ provider, you don't need to implement Where etc., that's already been done. I'd start there, before looking at any possible performance issues.

您需要创建一个的IQueryable 与您的自定义供应商进行解码前pression树等的特定逻辑这是相当复杂的,具有AA看 MSDN:演练:创建一个IQueryable LINQ提供或的编写自定义LINQ提供程序 ,有很多的教程左右。

You need to create an IQueryable with your custom provider to perform the specific logic of decoding the expression-tree etc. This is quite complicated, have a a look at MSDN: Walkthrough: Creating an IQueryable LINQ Provider or Writing custom LINQ provider , there's lots of tutorials around.

编辑:删除白痴改善

这篇关于落实byte []的LINQ查询我自己的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆