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

asp.net asp.net-mvc c# entity-framework linq

Question

Now that im trying the Right Join An Exception Popped up.

This is my Controller

   public IEnumerable<APPLICANT> GetApplicant()
    {
        IEnumerable<APPLICANT> applicantdata = Cache.Get("applicants") as IEnumerable<APPLICANT>;
        IEnumerable<Profile> profiledata = Cache.Get("profiles") as IEnumerable<Profile>;


        if (applicantdata == null)
        {

            var applicantList = (from a in context.Profiles 
                                 join app in context.APPLICANTs
                                 on a.PROFILE_ID equals app.Profile_id into joined
                                 from j in joined.DefaultIfEmpty()
                                 select new
                                            {
                                               APPLICANT = j, 
                                               Profile = a,
                                            }).Take(1000).AsEnumerable();

                   applicantdata = applicantList.AsEnumerable().ToList();


            if (applicantdata.Any())
            {
                Cache.Set("applicants", applicantdata, 30);
            }
        }
        return applicantdata;

    }

This is the error on

 applicantdata = applicantList.AsEnumerable().ToList();

Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable<Applicant.Models.APPLICANT>'. An explicit conversion exists (are you missing a cast?)

1
1
5/20/2013 8:50:54 AM

Popular Answer

applicantdata is IEnumerable<APPLICANT>, in your select statement you are selecting anonymous type object using new keyword, that is why you can't convert it to IEnumerable<APPLICANT>.

You have to create a temporay class with your properties as in select statement and return IEnumerable of that class.

Like:

public class MyClass
{
  public APPLICANT applicant {get;set;}
  public Profile porfile {get;set;}
}

Then modify your function to return IEnumerable<MyClass> like

public IEnumerable<MyClass> GetApplicant()
{
    IEnumerable<MyClass> applicantdata = Cache.Get("applicants") as IEnumerable<MyClass>;
    IEnumerable<Profile> profiledata = Cache.Get("profiles") as IEnumerable<Profile>;

    IEnumerable<MyClass> applicantList;
    if (applicantdata == null)
    {

        applicantList = (from a in context.Profiles 
                             join app in context.APPLICANTs
                             on a.PROFILE_ID equals app.Profile_id into joined
                             from j in joined.DefaultIfEmpty()
                             select new MyClass //Change here
                                        {
                                           APPLICANT = j, 
                                           Profile = a,
                                        }).Take(1000);

               applicantdata = applicantList.AsEnumerable();



        if (applicantdata != null && applicantdata.Any())
        {
            Cache.Set("applicants", applicantdata, 30);
        }
    }
    return applicantdata;

}

You can't project to APPLICANT, since that appears to be a class generated through entity framework.

2
5/20/2013 8:56:27 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow