有没有一种方法可以使用System.Data.Entity.Include方法使其强类型?在下面的方法中,Escalation是一个ICollection<>。
public IEnumerable<EscalationType> GetAllTypes() {
Database.Configuration.LazyLoadingEnabled = false;
return Database.EscalationTypes
.Include("Escalation")
.Include("Escalation.Primary")
.Include("Escalation.Backup")
.Include("Escalation.Primary.ContactInformation")
.Include("Escalation.Backup.ContactInformation").ToList();
}发布于 2012-06-01 09:28:50
这在Entity Framework4.1中已经可用。
有关如何使用include特性的参考信息,请参阅此处,它还展示了如何包含多个级别:http://msdn.microsoft.com/en-us/library/gg671236(VS.103).aspx
强类型的Include()方法是一个扩展方法,因此您必须记住声明using System.Data.Entity;语句。
发布于 2011-05-24 04:57:26
功劳归于Joe Ferner
public static class ObjectQueryExtensionMethods {
public static ObjectQuery<T> Include<T>(this ObjectQuery<T> query, Expression<Func<T, object>> exp) {
Expression body = exp.Body;
MemberExpression memberExpression = (MemberExpression)exp.Body;
string path = GetIncludePath(memberExpression);
return query.Include(path);
}
private static string GetIncludePath(MemberExpression memberExpression) {
string path = "";
if (memberExpression.Expression is MemberExpression) {
path = GetIncludePath((MemberExpression)memberExpression.Expression) + ".";
}
PropertyInfo propertyInfo = (PropertyInfo)memberExpression.Member;
return path + propertyInfo.Name;
}
}ctx.Users.Include(u => u.Order.Item)https://stackoverflow.com/questions/6102909
复制相似问题