LINQ动态数据模型

编程入门 行业动态 更新时间:2024-10-26 20:26:10
本文介绍了LINQ动态数据模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的问题是这个。我们的客户创建了他们想要收集的不同字段,我们允许他们构建动态过滤器,报告等... 我们运行一些TSQL来实际创建列,所有工作很好。我们现在为我们的过滤器增加了更多的功能,并且可以真正使用LINQ to SQL获得。 我已经尝试了动态Linq ( weblogs.asp/scottgu/archi...y-library.aspx ) 这正是我们需要让他们创建自己的过滤器和 这样的。但是,当我尝试过滤数据中不存在的列时,我会收到一个错误,这是可以理解的。 他们有一个表他们可以添加列,所以我将尝试 在运行LINQ 查询之前动态添加新的属性和属性。 例如,如果客户端添加一个名为Age of Int的新列,我想 向对象添加一个新属性。 所以它看起来我需要添加一个看起来像这样的属性: [Column(Storage =" Age",DbType =" Int NOT NULL")] public int年龄{get;设置;} 我一直在阅读不同的反思技巧和过去的例子 两天看起来好像我要绕圈子.. 有没有人对我如何做到这一点有任何建议?我非常感谢任何指示我正确方向的。

My problem is this. Our clients create different fields they want to collect and we allow them build dynamic filters, reports etc... We run some TSQL to actually create the column and all works very well. We are now adding a lot more functionality to our filters and could really benefit from using the LINQ to SQL. I have experimented with the Dynamic Linq (weblogs.asp/scottgu/archi...y-library.aspx) and it is exactly what we need to let them create their own filters and such. But when I try to filter on a column that does not exist in the Data Context I get an error, which is understandable. They have one table that they can add columns to, so I am going to attempt to dynamically add the new properties and attributes before running the LINQ query. For example, if a client adds a new column called Age of type Int, I want to add a new property to the object. So it looks like I need to add a property that looks something like: [Column(Storage="Age", DbType="Int NOT NULL")] public int Age { get; set;} I''ve been reading different reflection techniques and examples for the past two days and seems like I''m going around in circles.. Does anyone have any suggestions on how I might accomplish this? Anything to point me in the right direction would be greatly appreciated.

推荐答案

我有相反的经历。 我的意思是编码c#中已知的数据上下文,而不是 基类 - 这仍然是这种情况吗?或者您是否在运行时生成上下文 ? (在这种情况下,编码变得非常困难) 但是你的想法在这里肯定是有用的 - 我怀疑你花了更多的时间来获得这种情况比我们其他人工作 合并; -p Marc I have opposite experience. I was meaning coding against a known data-context in the c#, not a base-class - is this still the case? Or are you generating the context at runtime too? (in which case, coding gets very hard) But your thoughts are definitely useful here - I suspect you''ve spent more time trying to get this scenario working than the rest of us combined ;-p Marc

EDBrian写道: EDBrian wrote: 我应该使用Reflection.Emit来创建派生的 类型/属性吗?就像我在原帖中所说的那样,我一直在寻找反思并动态创建一个新的类,现在为几个b $ b天。你有任何建议或链接可能有帮助.... Should I use the Reflection.Emit to create the derived type/properties? Like I said in my original post, I''ve been looking into reflection and creating a new class dynamically for a couple days now. Do you have any suggestions or links that might help....

你如何在你的代码中引用这些新属性, 只在运行时出现? 运行时的动态列应该以不同的方式解决:有几个相关表格的b $ b,一个用于值和一个领域。查询方面有点困难,但这是可能的,因为程序中使用的语言是一种静态类型的语言。 FB - -------------------- -------------------------------------------------- - LLBLGen Pro的首席开发人员,.NET的高效O / R映射器 LLBLGen Pro网站: www.llblgen 我的.NET博客: weblogs.asp/fbouma Microsoft MVP(C#) ----------------------------------------------- -------------------------

And how are you going to refer to these new properties in your code, which only appear at runtime? Dynamic columns at runtime should be solved differently: with a couple of related tables, one for the values and one for the fields. It is a bit hard on the querying side, but it''s what''s possible as the language used in the program is a statically typed language. FB -- ------------------------------------------------------------------------ Lead developer of LLBLGen Pro, the productive O/R mapper for .NET LLBLGen Pro website: www.llblgen My .NET blog: weblogs.asp/fbouma Microsoft MVP (C#) ------------------------------------------------------------------------

我应该使用Reflection.Emit来创建派生类型/属性? Should I use the Reflection.Emit to create the derived type/properties? 就像我在原帖中说的那样,我一直在寻找反思和 动态创建新课程几天现在。 你有任何可能有帮助的建议或链接.... Like I said in my original post, I''ve been looking into reflection and creating a new class dynamically for a couple days now. Do you have any suggestions or links that might help....

正如我所写的那样,有比Reflection更简单的东西。发射: 1.动态编译以创建装配。我想我已经在这个新闻组中为Marc提前发布了一些样本 。 2.我刚刚发现你链接Dynamic Linq库CreateClass() 方法,非常感谢。 也许这可以使用,如果它允许指定基类和属性到 属性,任何经验? doc中的示例很有前途: DynamicProperty [] props = new DynamicProperty [] { new DynamicProperty (" Name",typeof(string)), new DynamicProperty(" Birthday",typeof(DateTime))}; Type type = DynamicExpression.CreateClass(道具); object obj = Activator.CreateInstance(type); t.GetProperty(" Name")。SetValue(obj," Albert",null) ; t.GetProperty(" Birthday")。SetValue(obj,new DateTime(1879,3,14),null); Console.WriteLine(obj) ; 我认为我们必须创建一些容易实现这一目标的课程。 Andrus。

As I wrote there are simpler things than Reflection.Emit: 1. Dynamic Compilation to create assembly. I think I have posted some sample earlier for Marc in this newsgroup. 2. I just discovered from you link Dynamic Linq library CreateClass() method, thanks a lot. Maybe this can be used, if it allows to specify base class and Attributes to properties, any experiences ? Sample in doc is very promising: DynamicProperty[] props = new DynamicProperty[] { new DynamicProperty("Name", typeof(string)), new DynamicProperty("Birthday", typeof(DateTime)) }; Type type = DynamicExpression.CreateClass(props); object obj = Activator.CreateInstance(type); t.GetProperty("Name").SetValue(obj, "Albert", null); t.GetProperty("Birthday").SetValue(obj, new DateTime(1879, 3, 14), null); Console.WriteLine(obj); I think we must create some class which allows this easily. Andrus.

更多推荐

LINQ动态数据模型

本文发布于:2023-11-04 05:49:59,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据模型   动态   LINQ

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!