帮助声明LINQ to EF查询 [英] help with declaration of LINQ to EF query
问题描述
大家好,谁可以帮助我定义LINQ查询.
我有一个简单的实体:
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();
每当我编译并调用查询时,都会收到错误消息:
在类型"System.Data.Entity.Infrastructure.DbQuery`1 [PhysicianRegistry.Data.]中声明的方法"System.Data.Entity.Infrastructure.DbQuery`1 [PhysicianRegistry.Data.PhysicianSpecialty] Include(System.String)". PhysicianSpecialty]''不能用类型为``System.Data.Objects.ObjectQuery`1 [PhysicianRegistry.Data.PhysicianSpecialty]''
的实例调用
谁可以解决我的问题?
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?
推荐答案
我自己解决了此问题.
导致其不起作用的主要原因是隐藏在属性的实现中:
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
});
IEnumerable的字符串表示形式的字符串如下所示:
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));
}
}
这篇关于帮助声明LINQ to EF查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!