如何使用C#Entity Framework在SQL Server表/视图等中获取每列的数据类型? [英] How can I get data type of each column in a SQL Server table/view etc. using C# Entity Framework?

查看:260
本文介绍了如何使用C#Entity Framework在SQL Server表/视图等中获取每列的数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Entity Framework获取SQL Server表或视图中的每一列的类型?

How can I get the type of each column in a SQL Server table or view using Entity Framework?

我需要这样做 BEFORE 我从表中获取所有数据,因为我想允许用户过滤,例如数据实际从SQL Server表/视图检索之前的日期。

I need to do this BEFORE I get all the data from the table, because I want to allow users to filter, for example, from a date before the data is actually retrieved from the SQL Server table/view.

所以如果我有一张具有发布日期的书籍的表格,我想返回每一列,发布者,发布日期)键入,以便事先进行过滤。我需要这个代码来做到这一点,因为我不一定知道列,因为用户可以使用几个不同的表。

So if I had a table of Books with a publish date, I would like to return each column (Name, Publisher, Publish Date) type in order to allow filtering beforehand. I need this code to do this because I will not necessarily know the columns, since the user may use several different tables.

.NET Framework类型很好,我不需要SQL Server类型...

The .NET Framework type is fine, I don't need the SQL Server type...

以下是一些示例代码:

using (var ArgoEntities = new ARGOEntities())
{
    //find the types here before the user performs the query so i can build the below code
    var query = from b in ArgoEntities.tbl_Books
                where b.PublishDate>[user specified date]  //some date here the user enters
                select b;

    var book = query.First();
}

编辑:我可以做到这一点,只有获得第一个记录在表,像这样...

I can do this so far only by getting the first record in the table, like this...

      using (ARGOEntities ArgoEntities = new ARGOEntities())
        {
            //var fred = typeof(ARGOEntities).GetProperties();
            //var fred = ArgoEntities.GetType().GetProperties();

            var a=ArgoEntities.tbl_Books.FirstOrDefault();
            var b = ObjectContext.GetObjectType(a.GetType());
            var c=b.GetProperties();
        }

但我重复一次,我不想先获得任何记录。 / p>

but i repeat, I DON'T want to get any records first.

推荐答案

Ryios的评论引用了我非常简单的答案,我知道它必须是,这将给我一个PropertyInfo的数组表中的每个字段。

Ryios' comment lead me to the extremely simple answer I knew it had to be, which will give me an array of PropertyInfo for each field in the table.

var columns=typeof(tbl_Books).GetProperties();

这篇关于如何使用C#Entity Framework在SQL Server表/视图等中获取每列的数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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