将 Linq 查询转换为 DataSet 失败

发布于 2024-11-04 23:42:04 字数 692 浏览 1 评论 0原文

我正在将旧的 .net 应用程序迁移到 .net 4,此迁移必须分几个阶段完成,这就是为什么某些方法可能看起来有点不传统的原因。无论如何...

我所拥有的是一个存储过程(Analysis_select),返回一行和几列的结果。如果我在一切正常的情况下调用它

var result = dbContext.Analysis_select(user.UserId, Year, Week);

,我可以使用调试器查看数据或将其显示在网格视图或类似的东西中,因此表达式和存储过程确实有效!但结果与代码的其余部分不兼容,所以...

如果我尝试将其转换为 DataSet 它会失败,Visual Studio 实际上说这是可以的,但在网页上渲染时它崩溃了

var result = (DataSet)dbContext.Analysis_select(user.UserId, Year, Week);

错误如下

无法将“SingleResult`1[Analysis_select]”类型的对象转换为“System.Data.DataSet”类型。

我读过一些从 linq 到 DataSet 的其他转换,但大多数方法对此似乎有点过多。我想保留DataSet的原因是,取决于这样的结果有数万行代码。确实很糟糕,但是你能帮我解决这个问题吗?

非常感谢任何帮助,谢谢!

I'm migrating an older .net application to .net 4, this migration has to be done in several stages, thats why some of the methods might seem a bit unconventional. Anyway...

What I have is a Stored Procedure (Analysis_select) returning one row with several columns with the result. If i call it with

var result = dbContext.Analysis_select(user.UserId, Year, Week);

everything is fine, i can view the data in with the debugger or display it in a grid view or something like that, so the expression and Stored Procedure really works! But the result is not compatible with the rest of the code so...

If I try to cast it to DataSet it fails, Visual Studio actually sais this is ok but when rendering on a web page it crashes

var result = (DataSet)dbContext.Analysis_select(user.UserId, Year, Week);

The error is as follows

Unable to cast object of type 'SingleResult`1[Analysis_select]' to type 'System.Data.DataSet'.

I've read about some other conversions from linq to DataSet but most of the methods seems a bit excessive for this. The reason why I want to keep the DataSet is that there's tens of thousands of lines of code depending on such results. Sucks yes, but can you help me fix this?

Any help is highly appreciated, thanks!

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

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

发布评论

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

评论(4

深空失忆 2024-11-11 23:42:04

我并不是建议这是一个很好的解决方案或最佳实践;肯定有一种不同的(而且可能更好)的方法。
如果您有 IEnumerable 并且没有其他方法来创建数据表,则可以使用反射。
你可以使用像下面这样的东西......

public static class ExtensionMethods
{
    public static DataTable ToDataTable<T>(this IEnumerable<T> items)
    {
        DataTable table = new DataTable();
        var properties = typeof(T).GetProperties();

        foreach (var propertyInfo in properties)
        {                
            table.Columns.Add(propertyInfo.Name, typeof(object));
        }

        foreach (var item in items)
        {
            var row = properties.Select(p => NormalizeObject(p.GetValue(item, null))).ToArray();                                
            table.Rows.Add(row);
        }

        return table;
    }

    private static object NormalizeObject(object value)
    {
        Binary bin = value as Binary;
        if (bin != null)
        {
            return bin.ToArray();
        }

        XElement element = value as XElement;
        if (element != null)
        {
            return element.ToString();
        }

        return value;
    }
}

I'm not suggesting this as a great solution or best practices; there is most definitely a different (and probably better) way.
For a case where you have IEnumerable and no other means to create a data table, reflection can step in.
You could use something like below...

public static class ExtensionMethods
{
    public static DataTable ToDataTable<T>(this IEnumerable<T> items)
    {
        DataTable table = new DataTable();
        var properties = typeof(T).GetProperties();

        foreach (var propertyInfo in properties)
        {                
            table.Columns.Add(propertyInfo.Name, typeof(object));
        }

        foreach (var item in items)
        {
            var row = properties.Select(p => NormalizeObject(p.GetValue(item, null))).ToArray();                                
            table.Rows.Add(row);
        }

        return table;
    }

    private static object NormalizeObject(object value)
    {
        Binary bin = value as Binary;
        if (bin != null)
        {
            return bin.ToArray();
        }

        XElement element = value as XElement;
        if (element != null)
        {
            return element.ToString();
        }

        return value;
    }
}
Hello爱情风 2024-11-11 23:42:04

您需要编写一个扩展方法来将 IEnumerable 转换为 DataSet。下面是如何将 IEnumerable 转换为 DataTable 的示例。

private DataTable ToDataTable<T>(List<T> items)
{
    var table = new DataTable(typeof (T).Name);

    PropertyInfo[] props = typeof (T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

    foreach (PropertyInfo prop in props)
    {
        Type t = GetCoreType(prop.PropertyType);
        table.Columns.Add(prop.Name, t);
    }

    foreach (T item in items)
    {
        var values = new object[props.Length];

        for (int i = 0; i < props.Length; i++)
        {
            values[i] = props[i].GetValue(item, null);
        }

        table.Rows.Add(values);
    }

    return table;
}
public static Type GetCoreType(Type t)
{
    if (t != null && IsNullable(t))
    {
        if (!t.IsValueType)
        {
            return t;
        }
        else
        {
            return Nullable.GetUnderlyingType(t);
        }
    }
    else
    {
        return t;
    }
}
public static bool IsNullable(Type t)
{
    return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
}

以下是该解决方案来源的链接:http://www.chinhdo。 com/20090402/convert-list-to-datatable/

You will need to write an extension method to convert the IEnumerable into a DataSet. Here is an example of how to convert IEnumerable to a DataTable.

private DataTable ToDataTable<T>(List<T> items)
{
    var table = new DataTable(typeof (T).Name);

    PropertyInfo[] props = typeof (T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

    foreach (PropertyInfo prop in props)
    {
        Type t = GetCoreType(prop.PropertyType);
        table.Columns.Add(prop.Name, t);
    }

    foreach (T item in items)
    {
        var values = new object[props.Length];

        for (int i = 0; i < props.Length; i++)
        {
            values[i] = props[i].GetValue(item, null);
        }

        table.Rows.Add(values);
    }

    return table;
}
public static Type GetCoreType(Type t)
{
    if (t != null && IsNullable(t))
    {
        if (!t.IsValueType)
        {
            return t;
        }
        else
        {
            return Nullable.GetUnderlyingType(t);
        }
    }
    else
    {
        return t;
    }
}
public static bool IsNullable(Type t)
{
    return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
}

Here's a link to the source of this solution: http://www.chinhdo.com/20090402/convert-list-to-datatable/

短叹 2024-11-11 23:42:04

您检查过本教程吗 http://msdn.microsoft.com/en-us/来自微软的library/bb386921.aspx?否则,LINQ 结果和数据集之间不会直接转换。

did you check this tutorial http://msdn.microsoft.com/en-us/library/bb386921.aspx from MS? Otherwise there is no direct conversion between LINQ result and Dataset.

拥抱我好吗 2024-11-11 23:42:04

使用 LINQ2SQL 存储过程,您永远不会获得数据集。您得到的正是 SingleResult。是一个 IEnumerable。

With LINQ2SQL stored procedures you never get DataSets. What you get is Exactly that, a SingleResult. Is an IEnumerable.

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