0

I have classes generated by Entity Framework

public partial class Sample
    {
        public int SamplesId { get; set; }
        public string Barcode { get; set; }
        public Nullable<System.DateTime> CreatedAt { get; set; }
        public Nullable<int> CreatedBy { get; set; }
        public Nullable<int> StatusId { get; set; }
    
        public virtual ICollection<Status> Status { get; set; }
        public virtual ICollection<User> User { get; set; }
    }
   public partial class Status
    {
        public int StatusId { get; set; }
        public string Status1 { get; set; }
        public virtual Sample Samples { get; set; }
    }
 public partial class User
    {
     
        public int UserId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public virtual Sample Samples { get; set; }
    }

And Repository class

public class SampleRepository
    {
         
        public IQueryable<Sample> GetAllSamples()
        {
            WebApiSampleEntities dbContext = new WebApiSampleEntities();
            return dbContext.Samples;
        }
         
    }

In the web API project, I have Sample Factory class as follows

 public class SampleFactory
    {
        public SampleModel Create(Sample sample)
        {
             return new SampleModel()
            {
                SamplesId = sample.SamplesId,
                Barcode = sample.Barcode,
                CreatedAt = sample.CreatedAt,
                CreatedBy = sample.CreatedBy,
                StatusId = sample.StatusId,
                User = sample.User.Select(s => Create(s))


            };
        }

But got the following error, please help me to resolve below issue on the line User = sample.User.Select(s => Create(s))

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<WebServiceSample.Models.UserModel>' to 'System.Collections.Generic.ICollection<DataAccessLibrary.User>'. An explicit conversion exists (are you missing a cast?)

Mrunal
  • 1
  • 1
  • 3
  • Any `Select()` return an `IEnumerabl` but your `User` is `ICollection` which is a mismatch. Also, `Samples` is a single `Sample` instance, but the return type of `GetAllSamples()` is `IQueryable`. So there's obviously a mismatch. – Sach Aug 03 '20 at 21:44
  • FYI, when naming a member that represents a collection, the name should be plural (i.e. `Users` would be a better name than `User` for the `ICollection` property). – Rufus L Aug 04 '20 at 00:16
  • @Sach, Should I change User as IEnumerable instead of ICollection? – Mrunal Aug 04 '20 at 13:45
  • That depends on your needs. Please read up here to understand the differences between `IEnumerable` and `IColllection`. https://medium.com/developers-arena/ienumerable-vs-icollection-vs-ilist-vs-iqueryable-in-c-2101351453db At the end of the day, you need those two to match, whichever you use. Or do a casting. – Sach Aug 04 '20 at 15:58
  • Further reading: https://stackoverflow.com/questions/10113244/why-use-icollection-and-not-ienumerable-or-listt-on-many-many-one-many-relatio – Sach Aug 04 '20 at 15:59

0 Answers0