Shihan Khan
Shihan Khan

Reputation: 2188

Convert System.Linq.IQueryable to System.Collections.Generic.ICollection

I'm new to asp.net mvc & I'm trying to make a website with asp.net mvc 4 & EF6 where user can sort a table after login. I'm getting a compile error saying Cannot implicitly convert type System.Linq.IQueryable to System.Collections.Generic.ICollection. My codes are below,

Controller

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");
        }
    }

Model

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

How can I solve this problem. Need this help badly. Tnx.

UPDATES

Mkistat Model

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; }        
}

Upvotes: 1

Views: 5517

Answers (2)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

You need to modify your model and set mkistats type to IQueryable<mkistat> , you are passing IQueryable<mkistat> where your property is of Type mkistat not IQueryable<mkistat>, you have to do like this:

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

and now in your action:

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

If you want to do with List<mkistat> htne your model should be like:

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

and in action:

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

Upvotes: 1

Tim
Tim

Reputation: 2912

Apparently mkistats are implementing I generic collection, and by calling .AsQueryable() you are making an iQueryable collection. If you need it to be queryable, change mkistats to implement iQueryable.

Otherwise you can call .toList() on it, but you lose queryable support and lazy loading, if those are important.

Edit: It looks like you don't need queryable, so just do this:

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

Likely you could also just leave both the .AsQueryable() and the .toList(). But it doesn't really matter and I can't decipher how your code fits together.

Upvotes: 0

Related Questions