Cannot implicity convert type ‘System.Collections.Generic.List’ to ‘System.Collections.Generic.IList

Having problem on my web api “Cannot implicity convert type IQueryable<> to Generic.List”. I’m getting the data from Entity Framework.
When I put ToList() it return this error: System.Collections.Generic.List’ to ‘System.Collections.Generic.IList<LM_WebApi.Controllers.Items>

Cannot implicity convert type 'System.Collections.Generic.List<string>' to 'System.Collections.Generic.IList<LM_WebApi.Controllers.Items>

    public IList<Sales_Data> GetSalesData()
    {
        using (var db = new LM_ReportEntities())
        {
            var list = db.OE_Invoiced_BaseProducts.Where(x => x.Year == "2020" && x.Period == 1);
            
            IList<Sales_Data> salesData = new List<Sales_Data>();

            salesData.Add(new Sales_Data { Items = list.Select(x => x.Item_Number).ToList() });
            salesData.Add(new Sales_Data { Periods = list.Select(x => x.Period) });

            return salesData;
        }
    }

Here are my class:

public class Items
{
    public string Item_No { get; set; }
}

public class Periods
{
    public string Period { get; set; }
}

My model:

public class Sales_Data
{
    public IList<Items> Items { get; set; } 
    public IList<Periods> Periods { get; set; }
}

I want to return the data from Items and Periods as List.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You have to do this in the following way:

var list = db.OE_Invoiced_BaseProducts.Where(x => x.Year == "2020" && x.Period == 1).ToList();

In the next step, populate the lists of Items and Periods types from list collection.

var ItemsList = new List<Items>();
var PeriodsList = new List<Periods>();
foreach (var i in list)
{
    Items item = new Items() { Item_No = i.Item_Number };
    Periods period = new Periods() { Period = i.Period };
    ItemsList.Add(item);
    PeriodsList.Add(period);
}

Finally, populate Sales_Data model by the above lists.

var salesData = new Sales_Data() { Items = ItemsList, Periods = PeriodsList };

Method 2

change db.OE_Invoiced_BaseProducts.Where(x => x.Year == "2020" && x.Period == 1) to db.OE_Invoiced_BaseProducts.Where(x => x.Year == "2020" && x.Period == 1).ToList()

Edit:

var list = db.OE_Invoiced_BaseProducts.Where(x => x.Year == "2020" && x.Period == 1);
...
salesData.Add(new Sales_Data { Items = list.Select(x => new Items{Item_No = x.Item_Number}).ToList() });
salesData.Add(new Sales_Data { Periods = list.Select(x => new Periods{Periods = x.Period}).ToList() });

Method 3

First of all, I think, you made a mistake in model, change it this way:

public class Sales_Data
{
    public string Item_No { get; set; }
    public string Period { get; set; }
}

And then change your method like this:

public IList<Sales_Data> GetSalesData()
{
    using (var db = new LM_ReportEntities())
    {
        var list = db.OE_Invoiced_BaseProducts.Where(x => x.Year == "2020" && x.Period == 1).ToList();
        
        return (from baseProduct in db.OE_Invoiced_BaseProducts
        where baseProduct.Year = "2020" 
            && baseProduct.Period == 1
        select new Sales_Data()
        {
            Item_No = baseProduct.Item_Number,
            Period = baseProduct.Period
        }).ToList();
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x