使用LINQ to SQL与动态表 [英] Using LINQ to SQL with Dynamic tables

查看:121
本文介绍了使用LINQ to SQL与动态表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个旧项目迁移到使用LINQ,但我遇到了一个很大的问题。问题是我们有动态表
用于搜索索引(具有动态属性的CM系统)。搜索索引具有每个可搜索属性{attribute_x,attribute_y,...}的列。
现在问题是我无法静态地定义哪些列是可用的(或者甚至在我们划分搜索索引时使用哪个表),所以我需要一种方法来实现。



我已经尝试使用动态表达式,但它们仍然需要一个类型来构建表达式,并且我无法通过反射生成有效的类型。 (似乎没有生成MemberInfo)。



我也可以满足于只需生成一个表达式进行搜索(但我猜这不是一件容易的事)。沿着

 
var mySearchIndex = db.GetTable(myTableType);
var query = from p in db.Products from idx in mySearchIndex;
query = query.Where(
idx。+ attributeName +> 50.0 &&
idx。+ attributeName +



/ p>

有没有人遇到这个问题的解决方案?过去两天我一直在浏览博客文章和论坛。

解决方案

你在Linq以外的工作到SQL的设计,但是我认为如果你真的需要,你最好的打算是探索XmlMappingSource,你可以在你的上下文的构造函数中使用它,当我们处理表重命名



在映射XML中,数据库中的实际列名称是列元素的Name属性,而class属性是Member,如果在XML it sho可以适当地映射最终查询。


I am trying to migrate an old project to using LINQ but I have run into a pretty major problem. The problem is we have dynamic tables for search indexing (CM-system with dynamic attributes). The search index has columns for each searchable attribute { attribute_x, attribute_y, ... }. Now the problem is I cannot statically define which columns are available (or even which table to use as we divide search indexes), so I need a way to do this on the fly.

I have tried using Dynamic Expressions, but they still require a type to build the expression from, and I haven't been able to generate a valid type by reflection. (It seems no MemberInfo is generated).

I could also satisfy with just being able to generate an expression for searching ( but I guess this is no easier task ). Something along the lines of

var mySearchIndex= db.GetTable(myTableType);
var query = from p in db.Products from idx in mySearchIndex;
query = query.Where(
 "idx." + attributeName + " > 50.0 &&
 idx." + attributeName + " 

would be desirable.

Has anyone come across a solution to this problem? I have been looking through blog posts and forums for the past two days in vain.

解决方案

You're working outside of Linq to SQL's design, but I think it could be done if you really need to. Your best bet to explore is probably the XmlMappingSource that you can use in your context's constructor. I had some success with this when it came to handling table renaming on the fly.

In mapping XML, the actual column name on the database is the "Name" attribute of the column element whereas the class property is the "Member". If you change that in the XML it should map the eventual query appropriately.

这篇关于使用LINQ to SQL与动态表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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