Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.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# 将System.Linq.IQueryable转换为System.Collections.Generic.ICollection_C#_Asp.net Mvc_Generics - Fatal编程技术网

C# 将System.Linq.IQueryable转换为System.Collections.Generic.ICollection

C# 将System.Linq.IQueryable转换为System.Collections.Generic.ICollection,c#,asp.net-mvc,generics,C#,Asp.net Mvc,Generics,我是asp.NETMVC的新手&我正在尝试使用asp.NETMVC4和EF6创建一个网站,用户可以在登录后对表格进行排序。我收到一个编译错误,它说无法隐式地将type System.Linq.IQueryable转换为System.Collections.Generic.ICollection。我的代码如下 控制器 public ActionResult Login(string sortOrder) { if (Session["UserNAME"] != null)

我是asp.NETMVC的新手&我正在尝试使用asp.NETMVC4和EF6创建一个网站,用户可以在登录后对表格进行排序。我收到一个编译错误,它说
无法隐式地将type System.Linq.IQueryable转换为System.Collections.Generic.ICollection
。我的代码如下

控制器

public ActionResult Login(string sortOrder)
    {
        if (Session["UserNAME"] != null)
        {
            ViewBag.CodeSort = String.IsNullOrEmpty(sortOrder) ? "code_desc" : "";
            var sortedOut = new MkistatVsUserLogin { mkistats = dsedb.mkistats.AsQueryable() };     //Error in this line

            switch (sortOrder)
            {
                case "code_desc":
                    sortedOut = sortedOut.OrderByDescending(s => s.MKISTAT_CODE);
                    break;
                default:
                    sortedOut = sortedOut.OrderBy(s => s.MKISTAT_CODE);
                    break;
            }
            return View(sortedOut.ToList());
        }
        else
        {
            return RedirectToAction("Home");
        }
    }
型号

public class MkistatVsUserLogin
{
    public mkistat mkistats { get; set; }
    public idx Idxs { get; set; }
}
public partial class mkistat
{
    public string MKISTAT_CODE { get; set; }
    public int MKISTAT_NUMBER { get; set; }
    public string MKISTAT_QUOTE_BASES { get; set; }
    public decimal MKISTAT_OPEN_PRICE { get; set; }
    public decimal MKISTAT_HIGH_PRICE { get; set; }
    public decimal MKISTAT_LOW_PRICE { get; set; }
    public decimal MKISTAT_SPOT_TOTAL_VALUE { get; set; }
    public string MKISTAT_LM_DATE_TIME { get; set; }        
}
我怎样才能解决这个问题。我非常需要这个帮助。Tnx

更新

Mkistat型号

public class MkistatVsUserLogin
{
    public mkistat mkistats { get; set; }
    public idx Idxs { get; set; }
}
public partial class mkistat
{
    public string MKISTAT_CODE { get; set; }
    public int MKISTAT_NUMBER { get; set; }
    public string MKISTAT_QUOTE_BASES { get; set; }
    public decimal MKISTAT_OPEN_PRICE { get; set; }
    public decimal MKISTAT_HIGH_PRICE { get; set; }
    public decimal MKISTAT_LOW_PRICE { get; set; }
    public decimal MKISTAT_SPOT_TOTAL_VALUE { get; set; }
    public string MKISTAT_LM_DATE_TIME { get; set; }        
}

显然,mkistats正在实现I泛型集合,通过调用.AsQueryable()您正在创建一个iQueryable集合。如果需要可查询,请将mkistats更改为实现iQueryable

否则,您可以对其调用.toList(),但如果这些都很重要,您将失去可查询支持和延迟加载

编辑:看起来您不需要queryable,所以只需执行以下操作:

var sortedOut = new MkistatVsUserLogin { mkistats = dsedb.mkistats.AsQueryable().toList() };

可能您也可以同时保留.AsQueryable()和.toList()。但是这并不重要,我也无法解释你的代码是如何组合在一起的。

你需要修改你的模型,并将
mkistats
类型设置为
IQueryable
,你正在传递
IQueryable
属性类型为
mkistat
而不是
IQueryable
,你必须这样做:

public class MkistatVsUserLogin
{
    public IQueryable<mkistat> mkistats { get; set; }
    public idx Idxs { get; set; }
}
如果您想使用
List
htne,您的模型应该是:

public class MkistatVsUserLogin
{
    public List<mkistat> mkistats { get; set; }
    public idx Idxs { get; set; }
}

mkistat
的定义是什么。你能用密码给我解释一下吗?这会很有帮助的。获取错误<代码>无法将类型“System.Collections.Generic.List”隐式转换为“MyMvcProj.Models.mkistat”