Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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:21am
Comments
Oleksandr Kulchytskyi at 30-Jul-12 8:28am
   
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 at 30-Jul-12 8:31am
   
Models.PhysicianInfo2.Speciality property that, represents physician info specialties, it could consists from one or more items.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
0 OriginalGriff 6,656
1 Sergey Alexandrovich Kryukov 6,400
2 CPallini 5,240
3 George Jonsson 3,584
4 Gihan Liyanage 2,625


Advertise | Privacy | Mobile
Web03 | 2.8.140921.1 | Last Updated 1 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100