Click here to Skip to main content
Sign Up to vote bad
good
Hi guys, who can help me with definition of LINQ query.
 
I've got a simple entity:
 
public class PhysicianInfo2
    {
        public PhysicianInfo2()
        {
            foreach (System.Reflection.PropertyInfo info in this.GetType().GetProperties())
            {
                if (info.PropertyType == typeof(List<string>))
                {
                    info.SetValue(this, new List<string>(), null);
                    continue;
                }
                info.SetValue(this, string.Empty, null);
            }
        }
 
        public string NPI { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string MiddleName { get; set; }
        public string Prefix { get; set; }
        public string Suffix { get; set; }
        public string Email { get; set; }
 
        //------- can be a list
        public string Speciality { get; set; }  //(this could be a list)
        public string Institution { get; set; } //(this could be a list)
        public string Status { get; set; }      //(online, offline, busy)
}
 
and query list below:
 
var item2 = (from item in _context.Physician.Include("PersonalInfo")
 
                             let specialities = (from spec in _context.PhysicianSpecialty.Include("Speciality")
                                                 where spec.PhysicianID == item.PhysicianID
                                                 select spec.Specialty)
 
                             select new Models.PhysicianInfo2()
                             {
                                 NPI = item.NPI,
                                 Email = item.PersonalInfo.Email,
                                 FirstName = item.PersonalInfo.FirstName,
                                 LastName = item.PersonalInfo.LastName,
                                 MiddleName = item.PersonalInfo.MiddleName,
                                 Prefix = item.PersonalInfo.Prefix,
                                 Suffix = item.PersonalInfo.Suffix,
                                 Speciality = (from subItem in specialities
                                               select subItem.Name).Aggregate((a, b) => string.Format("{0}, {1}", a, b)),
                                 Institution = string.Empty
                             });
 
                count = item2.AsEnumerable().Count();
 
Whenever i compile and invoke a query, i receive an error :
 
Method 'System.Data.Entity.Infrastructure.DbQuery`1[PhysicianRegistry.Data.PhysicianSpecialty] Include(System.String)' declared on type 'System.Data.Entity.Infrastructure.DbQuery`1[PhysicianRegistry.Data.PhysicianSpecialty]' cannot be called with instance of type 'System.Data.Objects.ObjectQuery`1[PhysicianRegistry.Data.PhysicianSpecialty]'
 

Who can resolve my issue?
Posted 30 Jul '12 - 2:21

Comments
Oleksandr Kulchytskyi - 30 Jul '12 - 8:28
partially, i resolved my issue , but i encountered with another problem related to aggregate exception Fixed query looks like below: var item2 = (from item in _context.Physician.Include("PersonalInfo").Include(x => x.PhysicianSpecialty).Include("PhysicianSpecialty.Speciality") let specialities = (from spec in _context.PhysicianSpecialty where spec.PhysicianID == item.PhysicianID select spec.Specialty) select new Models.PhysicianInfo2() { NPI = item.NPI, Email = item.PersonalInfo.Email, FirstName = item.PersonalInfo.FirstName, LastName = item.PersonalInfo.LastName, MiddleName = item.PersonalInfo.MiddleName, Prefix = item.PersonalInfo.Prefix, Suffix = item.PersonalInfo.Suffix, Speciality = (from subItem in specialities select subItem.Name).Aggregate((a, b) => string.Format("{0}, {1}", a, b)), Institution = string.Empty }); but error appears: LINQ to Entities does not recognize the method 'System.String Aggregate[String](System.Linq.IQueryable`1[System.String], System.Linq.Expressions.Expression`1[System.Func`3[System.String,System.String,System.String]])' method, and this method cannot be translated into a store expression.
Oleksandr Kulchytskyi - 30 Jul '12 - 8:31
Models.PhysicianInfo2.Speciality property that, represents physician info specialties, it could consists from one or more items.

1 solution

i have resolved this issue by myself.
 
The main reason , why this wont work , hide in realization of property:
public string Speciality { get; set; } 
 
As concerns this property i have change it type to, see below:
public IEnumerable<string> Specialty { get; set; } </string>
 
and changed a bit my query:
var item2 = (from item in _context.Physician.Include("PersonalInfo").Include(x => x.PhysicianSpecialty).Include("PhysicianSpecialty.Speciality")
 
                             let specialities = (from spec in _context.PhysicianSpecialty
                                                 where spec.PhysicianID == item.PhysicianID
                                                 select spec.Specialty)
 
                             select new Models.PhysicianInfo2()
                             {
                                 NPI = item.NPI,
                                 Email = item.PersonalInfo.Email,
                                 FirstName = item.PersonalInfo.FirstName,
                                 LastName = item.PersonalInfo.LastName,
                                 MiddleName = item.PersonalInfo.MiddleName,
                                 Prefix = item.PersonalInfo.Prefix,
                                 Suffix = item.PersonalInfo.Suffix,
                                 Specialty = specialities,
                                 Institution = string.Empty
                             });
 

String representation of IEnumerable of string irepresented like this:
 
public string SpecialityString 
{ 
get{return Specialty==null?string.empty:Specialty.Aggregate((a, b) =>                    
                                       string.Format("{0}, {1}", a, b));
 }
 }
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 586
1 Maciej Los 255
2 Slacker007 240
3 OriginalGriff 220
4 CPallini 210
0 Sergey Alexandrovich Kryukov 9,118
1 OriginalGriff 7,134
2 CPallini 3,803
3 Rohan Leuva 3,135
4 Maciej Los 2,558


Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 1 Aug 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid