将实体模型转换为数据集 - 这可以完成吗?

发布于 2024-11-08 05:01:57 字数 633 浏览 0 评论 0原文

这里非常沮丧... 我通常可以在网络上的某个地方找到 .Net 中复杂问题的某种答案,但我却找不到这个答案。 我所处的场景是,我必须将 LINQ to Entity 查询的结果转换为 DataSet,以便数据可以由现有业务逻辑进行处理,但我无法为此找到单一的工作解决方案。

我尝试过像 EntityCommand 生成读取器这样的基本方法,但是这个方法不起作用,因为 DataTable.Load() 会引发异常(EntityCommand 生成的读取器不支持 GetSchemaTable() )。

我还尝试了更多[据说]友好的方法,例如 Entity to IDataReader(http://l2edatareaderadapter.codeplex.com/),但是这个会抛出异常,文档很少,并且自 2008 年以来就没有被触及过。

另一种方法我发现在这里(http://blogs.msdn.com/b/alexj/archive/2007/11/27/Hydrating-an-entitydatareader-into-a-datatable-part-1.aspx),但没有工作副本代码;只是片段。

我很难相信,首先微软不会提供这种开箱即用的向后兼容项目,其次,它也不会是由社区创建的。

如果有可用的商业解决方案,我也愿意考虑。

谢谢!

Very frustrated here ...
I can usually find an answer of some kind to complex issues in .Net somewhere on the net, but this one eludes me.
I'm in a scenario where I have to convert the result of a LINQ to Entity query into a DataSet so the data can then be processed by existing business logic, and I can't find a single working solution out ther for this.

I've tried basic approaches like the EntityCommand generating a reader, but this one does not work because DataTable.Load() thorws an excpetion (the reader generated by EntityCommand does not support GetSchemaTable() ).

I've also tried more [supposedly] friendly approaches like Entity to IDataReader(http://l2edatareaderadapter.codeplex.com/), but this one throws exceptions, has very little docs, and hasn't been touched since 2008.

Another approach I found is here (http://blogs.msdn.com/b/alexj/archive/2007/11/27/hydrating-an-entitydatareader-into-a-datatable-part-1.aspx), but does not have a working copy of the code; only snippets.

I find it hard to believe that first of all MS would not have offered this backwards-compatibility item out of the box, and second, that it would not have been created by the community either.

I'm willing to look at commercial solutions as well if any are available.

Thx!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半暖夏伤 2024-11-15 05:01:57

您可以将结果转换为列表,并使用以下命令将列表转换为数据表。

public DataTable ConvertToDataTable<T>(IList<T> data)
    {
        PropertyDescriptorCollection properties =
           TypeDescriptor.GetProperties(typeof(T));
        DataTable table = new DataTable();
        foreach (PropertyDescriptor prop in properties)
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        foreach (T item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
                row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            table.Rows.Add(row);
        }
        return table;

    }

http://social.msdn.microsoft.com/Forums/br/csharpgeneral/thread/6ffcb247-77fb-40b4-bcba-08ba377ab9db

希望这对

Preetam有帮助

You can convert the result into a list and use the following to convert the list to a datatable.

public DataTable ConvertToDataTable<T>(IList<T> data)
    {
        PropertyDescriptorCollection properties =
           TypeDescriptor.GetProperties(typeof(T));
        DataTable table = new DataTable();
        foreach (PropertyDescriptor prop in properties)
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        foreach (T item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
                row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            table.Rows.Add(row);
        }
        return table;

    }

http://social.msdn.microsoft.com/Forums/br/csharpgeneral/thread/6ffcb247-77fb-40b4-bcba-08ba377ab9db

Hope this helps

Preetam

玩物 2024-11-15 05:01:57

这可能不是最好的解决方案,但如果您的场景只有一两个表需要添加到数据集中,为什么不直接手动构建它们。

var result = db.YourTable; // get your Linq To Entities result.

DataSet ds = new DataSet();
DataTable tbl = new DataTable();
tbl.Columns.Add("col1", typeof(string));
tbl.Columns.Add("col2", typeof(int));

foreach (var r in result)
{
  var row = tbl.NewRow();
  row[0] = r.Col1;
  row[1] = r.Col2;

  tbl.Rows.Add(r);

}

ds.Tables.Add(tbl);

Col1 和 Col2 来自 Linq To Entity 对象,您可以像这样创建所需的所有表并返回 DataSet。

This might not be the greatest solution, but if your scenario have only one or two table that you need to add to the DataSet, why not build them directly manually.

var result = db.YourTable; // get your Linq To Entities result.

DataSet ds = new DataSet();
DataTable tbl = new DataTable();
tbl.Columns.Add("col1", typeof(string));
tbl.Columns.Add("col2", typeof(int));

foreach (var r in result)
{
  var row = tbl.NewRow();
  row[0] = r.Col1;
  row[1] = r.Col2;

  tbl.Rows.Add(r);

}

ds.Tables.Add(tbl);

The Col1 and Col2 comes from your Linq To Entity objects, you can create all the table you need like this and return your DataSet.

§对你不离不弃 2024-11-15 05:01:57

这是一个灵活的代码,应该可以满足您的大部分需求:

public DataTable LINQToDataTable<T>(IEnumerable<T> varlist)
    {
        DataTable dtReturn = new DataTable();
        // column names
        PropertyInfo[] oProps = null;
        if (varlist == null) return dtReturn;
        foreach (T rec in varlist)
        {
            // Use reflection to get property names, to create table, Only first time, others will follow
            if (oProps == null)
            {
                oProps = ((Type)rec.GetType()).GetProperties();
                foreach (PropertyInfo pi in oProps)
                {
                    Type colType = pi.PropertyType;
                    if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
                    {
                        colType = colType.GetGenericArguments()[0];
                    }

                    dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
                }
            }

            DataRow dr = dtReturn.NewRow();
            foreach (PropertyInfo pi in oProps)
            {
                dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue
                (rec, null);
            }

            dtReturn.Rows.Add(dr);
        }

        return dtReturn;
    }

This is a flexible code and should handle most of your needs:

public DataTable LINQToDataTable<T>(IEnumerable<T> varlist)
    {
        DataTable dtReturn = new DataTable();
        // column names
        PropertyInfo[] oProps = null;
        if (varlist == null) return dtReturn;
        foreach (T rec in varlist)
        {
            // Use reflection to get property names, to create table, Only first time, others will follow
            if (oProps == null)
            {
                oProps = ((Type)rec.GetType()).GetProperties();
                foreach (PropertyInfo pi in oProps)
                {
                    Type colType = pi.PropertyType;
                    if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
                    {
                        colType = colType.GetGenericArguments()[0];
                    }

                    dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
                }
            }

            DataRow dr = dtReturn.NewRow();
            foreach (PropertyInfo pi in oProps)
            {
                dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue
                (rec, null);
            }

            dtReturn.Rows.Add(dr);
        }

        return dtReturn;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文