- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
背景
我在 Entity Framework 中创建了对象图,其中任何给定的对象 A 都会有一个表 Ac 来跟踪它的变化。这些对象也可能相互连接,例如 A 是 1-many 到 B。这是一个示例图:
A -> Ac
/ \
Bc <- B \
/ \
Cc <- C D -> Dc
我希望能够在某个时间点加载一个对象和特定的连接对象,方法是使用更改表来提取这些记录并应用它们。理想情况下,我希望能够使用或模仿 .Include
来自 Entity Framework 的功能。
问题
拉出哪些对象已包含在 IQueryable
中并不像我想象的那么容易。看着 IQueryable<T>
带有 T 的子对象 Include()
-ed,我可以看到这些关系存储在某种 Span
中Arguments
中的对象property - 但这些都是内部类,并且尝试检索此信息有很多步骤。
这是我目前所拥有的:
public static void LoadVersion<T>( this IQueryable<T> query, DateTime targetDateTime )
{
//grab the value of the "Arguments" property on query.Expression
//this has to be done through reflection because "Arguments" is not accessible otherwise
PropertyInfo argumentsPropertyInfo = query.Expression.GetType().GetProperties().FirstOrDefault( x => x.Name == "Arguments" );
dynamic argumentsPropertyValue = argumentsPropertyInfo.GetValue( query.Expression );
for (int i = 0; i < argumentsPropertyValue.Count; i++)
{
//This gets me a System.Data.Entity.Core.Objects.Span, but that class is internal
//In the watch, I can see span -> SpanList[0].Navigations[0] gives me the name of the class in the .Include()
// This is the value I need
dynamic span = argumentsPropertyValue[i].Value;
//So if I try to pull it out using the same reflection trick as before, I get
// a dynamic {System.Reflection.PropertyInfo[0]} (not a list, as you would normally expect),
// and accessing those values & methods makes the debugger exit without an exception
dynamic spanPropertyInfo = argumentsPropertyValue[i].Value.GetType().GetProperties();
//this makes the debugger exit without an exception
dynamic spanPropertyValue = spanPropertyInfo[0].GetValue(span);
//this also makes the debugger exit without an exception (with the above line commented out, of course)
dynamic spanPropertyValue2 = spanPropertyInfo.GetValue( span );
}
}
基于我很难找到包含在查询中的内容,我不禁认为我这样做完全是错误的。深入研究 Entity Framework 6.1.3 的一些源代码并未对此有太多了解。
我一直在研究 Alex Derck 提供的代码,但我意识到我仍然需要一些代码才能按照我想要的方式完成这项工作。
这是 VisitMethodCall
的版本我实现了:
protected override Expression VisitMethodCall( MethodCallExpression node )
{
if (node.Method.Name != "Include" && node.Method.Name != "IncludeSpan") return base.VisitMethodCall(node);
try
{
string includedObjectName = (string) node.Arguments.First().GetPrivatePropertyValue( "Value" );
if (includedObjectName != null)
{
_includes.Add(includedObjectName);
}
}
catch (Exception e ){ }
return base.VisitMethodCall( node );
}
我能够使用包含构建查询并使用 IncludeVisitor 获取我包含的对象的名称,但使用这些的主要目标是能够找到相关表并将它们添加到包含。
所以当我有这样的等价物时:
var query = ctx.Persons.Include(p => p.Parents).Include(p => p.Children);
// includes[0] = "Parents"
// includes[1] = "Children"
var includes = IncludeVisitor.GetIncludes(query.Expression);
我成功抢到了includes
,然后我可以找到相关表(Parents -> ParentsChanges,Children -> ChildrenChanges),但我不是 100% 确定如何将这些添加回包含。
这里的主要问题是当它是嵌套语句时: context.A.Include(x => x.B).Include(x => x.C).Include(x => x.B.Select(y => y.D))
我可以成功遍历整个图并获得 A、B、C 和 D 的名称,但我需要能够添加这样的语句返回到包含:
[...].Include(x => x.B.Select(y => y.D.Select(z => z.DChanges)))
我可以很好地找到 DChanges,但我不知道如何构建包含备份的内容,因为我不知道 DChanges 和原始项目 (A) 之间有多少步骤。
最佳答案
在 source code 中查看了一下之后我注意到 Entity Framework 的包含不是 Expression
的一部分,而是 IQueryable
的一部分。如果你考虑一下,很明显它应该是那样的。表达式本身不能实际执行代码,它们由提供者翻译(这也是 IQueryable
的一部分),并非所有提供者都应该知道如何翻译 Include
方法。在源代码中,您可以看到 IQueryable.Include
方法调用了以下小方法:
public ObjectQuery<T> Include(string path)
{
Check.NotEmpty(path, "path");
return new ObjectQuery<T>(QueryState.Include(this, path));
}
查询(转换为 ObjectQuery
)只是被返回并且只有它的内部 QueryState
被改变,表达式没有任何变化。在调试器中,如果您查看 IQueryable
,您可以看到将包含的 EntitySet,但我无法将它们放入列表(_cachedPlan
始终当我尝试通过反射访问它时为 null)。
我认为在看到这个之后,你试图做的事情是不可能的,所以我会在我的 dbContext 中保留一个静态字符串列表并实现一个自定义的 Include
扩展方法:
public partial class TestDB
{
public static ICollection<Expression> Includes { get; set; } = new List<Expression>();
public TestDB() : base()
{
Includes = new List<Expression>();
}
...
}
public static class EntityExtensions
{
public static IQueryable<T> CustomInclude<T, TProperty>(this IQueryable<T> query,
Expression<Func<T,TProperty>> include) where T : class
{
TestDB.Includes.Add(include);
return query.Include(include);
}
}
您还可以“覆盖”System.Data.Entity
中的普通Include
方法。我说“覆盖”,因为从技术上讲,实际上不可能覆盖扩展方法,但您可以自己创建一个名为 Include
的扩展方法,如果您不包含 >System.Data.Entity
在您使用它的地方,您自己的方法与来自 System.Data.Entity
的方法之间没有歧义:
public static class EntityExtensions
{
public static IQueryable<T> Include<T, TProperty>(this IQueryable<T> query,
Expression<Func<T,TProperty>> include) where T : class
{
TestDB.Includes.Add(include);
var method = typeof(QueryableExtensions)
.GetMethods(BindingFlags.Public | BindingFlags.Static)
.Where(m => m.Name == "Include")
.First(m => m.GetParameters().All(p => p.ParameterType.IsGenericType));
var generic = method.MakeGenericMethod(typeof(T), typeof(TProperty));
return (IQueryable<T>)generic.Invoke(query, new object[] { query, include });
}
}
关于c# - 获取添加到 Entity Framework 6 的对象列表包含列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37991078/
如标题所示,ans_list是一个答案列表,ans_index是一个数字(答案在词汇表中的索引,但与atm无关) 这里生成的 tree.anslist 是什么? (例如,仅针对第一个),忽略迭代。 f
我目前将用户的输入存储在逗号分隔的列表中,如下所示: Userid | Options 1 | 1,2,5 用户在一个数组形式中勾选一组选项,然后用逗号连接起来 1,2,5 然后 MySQ
我目前将用户的输入存储在逗号分隔的列表中,如下所示: Userid | Options 1 | 1,2,5 用户在一个数组形式中勾选一组选项,然后用逗号连接起来 1,2,5 然后 MySQ
我想知道如何完全展平列表和包含它们的东西。除其他外,我想出了一个解决方案,它可以将具有多个元素的东西滑倒并将它们放回原处,或者在滑倒后将具有一个元素的东西拿走。 这与 How do I “flatte
我想知道如何完全展平列表和包含它们的东西。除其他外,我想出了一个解决方案,它可以将具有多个元素的东西滑倒并将它们放回原处,或者在滑倒后将带有一个元素的东西拿走。 这与 How do I “flatte
这个问题已经有答案了: Convert nested list to 2d array (3 个回答) 已关闭 7 年前。 java中有没有快捷方式可以转换 List> 到 String[][] ?
我在排序时遇到问题 List> 。我创建了一个自定义比较器,在其中编写了对数据进行排序的代码。 public class CustomComparator implements Comparator
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: Java Generics: Cannot cast List to List? 我只是想知道为什么下面的java代
试图想出一个 LINQy 方法来做到这一点,但我什么也没想到。 我有一个对象列表<>,其中包含一个属性,该属性是逗号分隔的字母代码列表: lst[0].codes = "AA,BB,DD" lst[1
假设我有这些任务: points = [] point = (1, 2) 我怎么会这样做: points += point 它工作得很好,并且给了我点 = [1, 2]。但是,如果我这样做: poin
如何在 scala 中将 List[Task[List[Header]]] 类型转换为 Task[List[Header]]。 我有一个方法返回 Task[List[Header]] 并多次调用 do
如何在 Java 中查找二维列表的元素? 我有一个参数为 List> 的函数我想知道如何找到这个列表的行和列。 最佳答案 如果你喜欢 List> obj 然后你就可以像这样访问 obj.get(cur
分配 List到 List工作正常。 分配 List>到 List>不编译。 代码 public class Main { public static void main(String[] a
我正在用 Java 编写一个方法,该方法必须接收并迭代 Serializable 的 List。 有什么区别: public void myMethod(List list) { } 和 public
我看到很多人想用 mvvm 更新网格/列表/树的一部分,但他们不想刷新整个列表。 对于所有遇到此问题的人,我做了以下示例。 希望这对你有用。 最佳答案 这是一个简单的例子。整个代码中最重要的是: Bi
我正在为现有的 C++ 库编写包装器,该库使用列表,其中 T 是自定义结构。我被建议使用 vector 而不是列表,但我试图避免修改库。 为了更好地理解这个场景,我做了一个简单的应用程序,使用一个列表
List list List list 这两种声明有什么区别吗? 谢谢, 最佳答案 是的。 List可以包含所有派生自 Base 的不同事物的混合物. List包含同质项(从某种意义上说,它们必须全部
有人可以尽可能详细地解释以下类型之间的区别吗? List List List 让我更具体一点。我什么时候想使用 // 1 public void CanYouGiveMeAnAnswer(List l
我有一个元组列表,每个元组都是一对列表。所以我的数据看起来像: mylist = [(['foo', 'bar'], ['bar', 'bar']),(['bar', 'bar'],['bar', '
也许是一个时髦的标题,但我遇到了以下问题: 给定一个类型为 (a * b) list 的列表,我想创建一个类型为 (a * b list) list 的新列表。一个例子: 给定列表 let testL
我是一名优秀的程序员,十分优秀!