在C#中从LINQ提取SQL列扩展属性 [英] Extract SQL Column Extended Properties From LINQ in C#

查看:101
本文介绍了在C#中从LINQ提取SQL列扩展属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SQL表,每列都有扩展属性.

i have an SQL table with extended properties on each column.

是否可以使用Linq2SQL在c#中从LINQ访问这些文件?

Is there a way to access these from LINQ in c# using Linq2SQL?

推荐答案

诸如"MS_Description"之类的东西?不是AFAIK;您可以编写一个与数据库的存储进行对话的SP(获取数据库对象名称和列名称),然后通过数据上下文进行查询-但没有内置.

Things like "MS_Description" etc? Not AFAIK; you could write an SP that talks to the store at the database (taking the database object name and column name), and query that via the data-context - but nothing built in.

编写一些使用Expression来获取要传递的数据库名称(而不是OO名称)的代码也将非常容易.

It would also be pretty easy to write some code that uses Expression to get the database names (instead of the OO names) to pass in. Something like:

    public static string GetProperty<TContext, TValue>(
        this TContext ctx, Expression<Func<TContext, TValue>> selector,
        string propertyName)
        where TContext : DataContext
    {
        MemberExpression me = selector.Body as MemberExpression;
        if (me == null) throw new InvalidOperationException();
        var member = me.Member;
        var objType = me.Expression.Type;
        var metaType = ctx.Mapping.GetMetaType(objType);
        string tableName = metaType.Table.TableName;
        string columnName = metaType.GetDataMember(member).MappedName;

        return ctx.GetProperty(tableName, columnName, propertyName);
    }

(或类似的东西;只是启动一个测试数据库...)

(or something similar; just firing up a test db...)

通过映射的SPROC提供GetProperty方法的位置.

Where you provide the GetProperty method via a mapped SPROC.

更新:是的,那有点奏效;例如:

Update: yes, that kinda works; example:

string desc = ctx.GetProperty(x => x. DataChanges.First().Change, "MS_Description");

First()很痛苦,但是比必须要有两个选择器要丑陋得多.但是可以用这种方式重写它:

The First() is a pain, but less ugly than having to have two selectors; it could be re-written that way, though:

string desc = ctx.GetProperty(x => x.DataChanges, dc => dc.Change, "MS_Description");

由您决定,这不是那么简单.另请注意,您需要将所有者从tableName中分离出来.

Up to you which is less hacky. Note also that you'd need to split the owner out of tableName.

这篇关于在C#中从LINQ提取SQL列扩展属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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