Q:

C# Linq query not returning an Enumerable collection

I am new C# and EF and LINQ
I am trying to learning (much like a toddler) how to do this so apologies if this is obvious
I am getting a type conversion error when i create a LINQ query
namespace ABC.Pages
{
    public partial class AOmMainComponent
    {
        [Inject]
        MydbContext MydbContext { get; set; }

    public IEnumerable<OMSite> OMSites()
    {
            var Sites = from s in MydbContext.TblOms select new {SiteId = s.OMID, SiteName = s.OMSite };

        return (IEnumerable<OMSite>)Sites;

    }
}

public class OMSite
    {
        public int SiteID { get; set; }
        public string SiteName { get; set; }
    }
}

I've tried:
return Sites;

and I've tried
return (IEnumerable<OMSite>)Sites;

but neither work.Please can someone advise what i'm doing wrong?
thanks
john

A:

Change:
var Sites = from s in MydbContext.TblOms select new {SiteId = s.OMID, SiteName = s.OMSite };

To:
var Sites = from s in MydbContext.TblOms select new OMSite {SiteId = s.OMID, SiteName = s.OMSite };

Particulary select new { -> select new OMSite{. After that return Sites; should work.
Your current code creates IEnumerable of anonymous types which can not be cast to  IEnumerable<OMSite> which your IEnumerable<OMSite> OMSites() method expects to be returned.

