Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi guys, who can help me with definition of LINQ query.

I've got a simple entity:

C#
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:

C#
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
Comments
Oleksandr Kulchytskyi 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 30-Jul-12 8:31am    
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:
C#
public string Speciality { get; set; } 


As concerns this property i have change it type to, see below:
C#
public IEnumerable<string> Specialty { get; set; } </string>


and changed a bit my query:
C#
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:

C#
public string SpecialityString 
{ 
get{return Specialty==null?string.empty:Specialty.Aggregate((a, b) =>                    
                                       string.Format("{0}, {1}", a, b));
 }
 }
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900