1

I hope someone can help me because when i edit my code . an exception shows

THIS IS MY CONTROLLER

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

    if (applicantdata == null)
    {
        var data = from app in context.APPLICANTs
                   join a in context.Profiles
                   on app.Profile_id equals a.PROFILE_ID into output
                   from j in output.DefaultIfEmpty()
                   select new { 
                       Id = app.APPLICANT_ID, 
                       LastName = 
                           (j == null ? app.APPLICANT_LastName : j.Applicant_LASTNAME) 
                   };

        var applicant = data
            .Where(v => !String.IsNullOrEmpty(v.LastName))
            .Take(1000);

        applicantdata = (from a in applicant
                         select new APPLICANT() { 
                             APPLICANT_ID = a.Id, 
                             APPLICANT_LastName = a.LastName
                         }).AsEnumerable();

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

    return applicantdata;    
}

This is the exception:

NotSupportedException was unhandle by user code. The entity or complex type ' ' not be constructed in a LINQ to Entities query. LINQ ASP.NET

At this line:

if (applicantdata.Any())
Jim D'Angelo
  • 3,914
  • 3
  • 22
  • 38
Enrique Gil
  • 734
  • 2
  • 17
  • 42
  • On the line `if (applicantdata.Any())`, are you trying to check if the operation returned records? – whastupduck May 22 '13 at 02:20
  • 1
    You can't project into a mapped entity. http://stackoverflow.com/a/5325861/1914530 –  May 22 '13 at 02:31
  • @GianAcuna yes sir that is the function of `if (applicantdata.Any())` – Enrique Gil May 22 '13 at 03:18
  • @bmused but how can i implement it? – Enrique Gil May 22 '13 at 03:19
  • 1
    It's unclear what you are trying to do. You appear to be trying to get EF entities of type `APPLICANT` but for some reason you join and project into an anonymous type. You then try and construct a new `APPLICANT` from the data in your anonymous type. I suggest you look [here](http://msdn.microsoft.com/en-us/library/bb399367.aspx) for examples on how to write LINQ to Entities queries. –  May 22 '13 at 04:11

1 Answers1

0

In Linq-to-Entities you can only project to any existing mapped entity type but you can project to an anonymous type

applicantdata = (from a in applicant
                     select new APPLICANT() { 
                         APPLICANT_ID = a.Id, 
                         APPLICANT_LastName = a.LastName
                     }).AsEnumerable();

In the above code you are trying to project to 'APPLICANT' type so it will not allow you to do so. you can try to do it using anonymous type as :

applicantdata = (from a in applicant
                     select new { 
                         APPLICANT_ID = a.Id, 
                         APPLICANT_LastName = a.LastName
                     }).AsEnumerable();

Hope this will help!

Sanjeev Rai
  • 6,019
  • 4
  • 20
  • 33