-3

I have an error:

(Cannot implicitly convert type 'shenoy webapi.Models.PartIndex' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?) shenoywebapi D:\shenoystudio\shenoywebapi\Controllers\RateController.cs 69 Active)

[Route("api/Rate/getproductrate")]
public IEnumerable<Rate> GetProductRate()
{
    var list = new List<Rate>();
    var dsRate = SqlHelper.ExecuteDataset(AppDatabaseConnection, CommandType.StoredProcedure, 0, "GetProductRates");

    if (dsRate.Tables[0].Rows.Count > 0)
    {
        foreach (DataRow row in dsRate.Tables[0].Rows)
        {
            list.Add(new Rate
            {
                Id = Convert.ToInt64(row[0]),
                SerialNumber = row[1].ToString(),
                ProductName = row[2].ToString(),
                Unit = row[3].ToString(),
                PartIndex = new PartIndex { Series = row[4].ToString() },
            });
        }
    }
    return list;
}

This is my model:

namespace shenoywebapi.Models
{
    public class Rate
    {
        public long Id { get; set; }
        public DateTime wefDate { get; set; }
        public string SerialNumber { get; set; }
        public string ProductName { get; set; }
        public string Unit { get; set; }
        public long Rates { get; set; }
        public IEnumerable<PartIndex> PartIndex { get; set; }
    }
}

Severity Code Description Project File Line Suppression State Error CS0266 Cannot implicitly convert type 'shenoywebapi.Models.PartIndex' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?) shenoywebapi D:\shenoystudio\shenoywebapi\Controllers\RateController.cs 69 Active

Peter B
  • 22,460
  • 5
  • 32
  • 69
Faiz Shaikh
  • 3
  • 1
  • 6
  • Can you pinpoint which line is line 69? – pappbence96 Jul 19 '19 at 07:32
  • Do you intend the `PartIndex` property to be a reference to a single `PartIndex` object, or a collection of them? Your `Rate` class expects a collection, but you're specifying a single value. – Jon Skeet Jul 19 '19 at 07:36

3 Answers3

0

In your Rate class you have a property defined as:

public IEnumerable<PartIndex> PartIndex { get; set; }  

In your function, you are trying to assign it as:

PartIndex = new PartIndex { Series = row[4].ToString() } 

So you're trying to assign an object instead of an IEnumerable. Try to wrap the assignment in a new List() {...} to fix it.
The error also tells you about it pretty clearly:

Cannot implicitly convert type 'PartIndex' to 'System.Collections.Generic.IEnumerable'

Which means it cannot automatically cast a single object into an IEnumerable containing only that one object. You have to do it manually.

pappbence96
  • 1,164
  • 2
  • 12
  • 20
0

It's this line

PartIndex = new PartIndex { Series = row[4].ToString() }

in your model PartIndex must be IEnumerable<PartIndex> it must be

PartIndex = new List<PartIndex>() { new PartIndex() {Series = row[4].ToString()} }
Dale K
  • 25,246
  • 15
  • 42
  • 71
whizzle
  • 50
  • 5
0

First IEnumerable<PartIndex> can be create by List<PartIndex> So create IEnumerable like

PartIndex = new List<PartIndex>();

And insert your element like :

PartIndex = new List<PartIndex>() { new PartIndex() {Series = row[4].ToString()} }

Last you can look that , It talk about IEnumrable and List . IEnumerable vs List - What to Use? How do they work?

TimChang
  • 2,249
  • 13
  • 25