Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将Linq查询转换为数据集失败_C#_Asp.net_Linq_Stored Procedures_Dataset - Fatal编程技术网

C# 将Linq查询转换为数据集失败

C# 将Linq查询转换为数据集失败,c#,asp.net,linq,stored-procedures,dataset,C#,Asp.net,Linq,Stored Procedures,Dataset,我正在将一个较旧的.net应用程序迁移到.net 4,这个迁移必须分几个阶段完成,这就是为什么有些方法看起来有点不传统的原因。无论如何 我拥有的是一个存储过程(Analysis_select),返回一行多列的结果。如果我把它叫做 var result = dbContext.Analysis_select(user.UserId, Year, Week); 一切都很好,我可以使用调试器在中查看数据,或者在网格视图或类似的视图中显示数据,所以表达式和存储过程真的可以工作!但是结果与代码的其余部分

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

我拥有的是一个存储过程(Analysis_select),返回一行多列的结果。如果我把它叫做

var result = dbContext.Analysis_select(user.UserId, Year, Week);
一切都很好,我可以使用调试器在中查看数据,或者在网格视图或类似的视图中显示数据,所以表达式和存储过程真的可以工作!但是结果与代码的其余部分不兼容,所以

如果我尝试将其转换为数据集,它会失败,VisualStudio实际上会说这没问题,但在网页上呈现时会崩溃

var result = (DataSet)dbContext.Analysis_select(user.UserId, Year, Week);
错误如下

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

我读过一些从linq到DataSet的其他转换,但大多数方法对此似乎有点过分。我之所以要保留数据集,是因为根据这样的结果有成千上万行代码。糟透了是的但你能帮我修一下吗


非常感谢您的帮助,谢谢

您是否查阅了MS的本教程?否则,LINQ结果和数据集之间没有直接转换。

您是否查阅了MS提供的本教程?否则,LINQ结果和数据集之间没有直接转换。

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

使用LINQ2SQL存储过程,您永远无法获得数据集。你得到的正是这个,一个单一的结果。是一个IEnumerable。

我并不是说这是一个很好的解决方案或最佳实践;肯定有一种不同(可能更好)的方法。 对于有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;
    }
}
公共静态类扩展方法
{
公共静态数据表到数据表(此IEnumerable items)
{
DataTable=新的DataTable();
var properties=typeof(T).GetProperties();
foreach(属性中的var propertyInfo)
{                
table.Columns.Add(propertyInfo.Name,typeof(object));
}
foreach(项目中的var项目)
{
var row=properties.Select(p=>NormalizeObject(p.GetValue(item,null)).ToArray();
table.Rows.Add(行);
}
返回表;
}
私有静态对象规范化对象(对象值)
{
Binary bin=作为二进制的值;
如果(bin!=null)
{
返回bin.ToArray();
}
XElement元素=作为XElement的值;
if(元素!=null)
{
返回元素.ToString();
}
返回值;
}
}

我并不认为这是一个很好的解决方案或最佳实践;肯定有一种不同(可能更好)的方法。 对于有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;
    }
}
公共静态类扩展方法
{
公共静态数据表到数据表(此IEnumerable items)
{
DataTable=新的DataTable();
var properties=typeof(T).GetProperties();
foreach(属性中的var propertyInfo)
{                
table.Columns.Add(propertyInfo.Name,typeof(object));
}
foreach(项目中的var项目)
{
var row=properties.Select(p=>NormalizeObject(p.GetValue(item,null)).ToArray();
table.Rows.Add(行);
}
返回表;
}
私有静态对象规范化对象(对象值)
{
Binary bin=作为二进制的值;
如果(bin!=null)
{
返回bin.ToArray();
}
XElement元素=作为XElement的值;
if(元素!=null)
{
返回元素.ToString();
}
返回值;
}
}

您需要编写一个扩展方法来将IEnumerable转换为数据集。下面是一个如何将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<>));
}
私有数据表到数据表(列表项)
{
var table=新数据表(typeof(T).Name);
PropertyInfo[]props=typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach(PropertyInfo props in props)
{
类型t=GetCoreType(prop.PropertyType);
表.列.添加(道具名称,t);
}
foreach(项目中的T项目)
{
var值=新对象[props.Length];
for(int i=0;i

这里有一个指向此解决方案来源的链接:

您需要编写一个扩展方法来将IEnumerable转换为数据集。下面是一个如何将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<>));
}
私有数据表到数据表(列表项)
{
var table=新数据表(typeof(T).Name);
PropertyInfo[]props=typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach(PropertyInfo props in props)
{
类型t=GetCoreType(prop.PropertyType);
表.列.添加(道具名称,t);
}
foreach(项目中的T项目)
{
var值=新对象[props.Length];
对于(int i=0;i