获得"&的indexOf QUOT;在实体框架的ICollection的中 [英] Get the "indexOf" of an ICollection in Entity Framework

查看:83
本文介绍了获得"&的indexOf QUOT;在实体框架的ICollection的中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有连接的方式,这样对象A 两个对象包含一个的ICollection <$ C $的C>对象B 。我希望能够确定在的ICollection&LT内的indexOf对象B;对象B&GT; 存储在对象A

I have two objects that are connected in a way such that ObjectA contains an ICollection of ObjectB. I would like to be able to determine the indexOf ObjectB in inside the ICollection<ObjectB> that is stored in ObjectA.

其中一种解决方案是对ICollection转换为一个列表,然后使用内置的IndexOf。然而,当多个线程访问ICollection的,在许多情况下,我可以得到多个ObjectBs相同的indexOf值

One of the solutions was to convert the ICollection to a list and then use the built in IndexOf. However, when multiple threads access the ICollection, in many cases, I can get the same indexOf value for multiple ObjectBs.

有没有什么办法让某个领域(的类型为int),存储对象B的ICollection的内部指标?如果没有,反正是有保证的indexOf(当多个线程试图访问它)给出正确的指标(即无螺纹的事)?

Question Is there any way to have a certain field (of type int) that stores the index of ObjectB inside the ICollection? If not, is there anyway to ensure that indexOf (when multiple threads attempt to access it) gives the right index (i.e. no matter of the thread)?

可能的解决方案我试图以确保使用新的环境对每个查询以及 GetDatabaseValues​​()刷新()。这更好的工作(尤其是在调试模式下),但在调试模式被关闭,价值相同的索引被赋予更多的对象B 秒。

Possible solutions I've tried to ensure to use a new context for each look up as well as GetDatabaseValues() and Reload(). This has worked better (especially in Debug mode), but when the debug mode is turned off, the same index of value is given to more ObjectBs.

我尝试添加一个排序依据的语句,但似乎没有办法工作。

I tried to add an OrderBy statement, but it seems like none of the approaches work.

// var objectB  = new ObjectB();
using(var context = new ContextDb())
{
    var objectA = context.ObjectAs.Single(x => x.Id == 1);

    objectA.objectBs.Add(objectB);

    context.SaveChanges();
    context.Entry(objectB).Reload();
    context.Entry(objectA).Reload();


    var list = objectA.objectBs.Select(x => x.Id).OrderBy(x => x).ToList(); // order by primary key.
    sb.AppendLine( string.Join(",", list.ToArray())); // for testing
    objectB.LocalId= list.IndexOf(objectB.Id) + 1; // the "local id"

    context.SaveChanges();
}

其结果是很奇怪的,但我似乎能够看到一种模式。说明,code以上是在for循环中运行一定的时间。在第一次迭代(在字符串生成器第一行)给出了以下内容:

The result is quite strange, although I seem to be able to see a pattern. Note, the code above is in a for loop that runs a certain amount of times. During the first iteration (first line in the string builder) gives the following:


2932 2932,2933 2932,2933,2934 2932,2933,2934,2935,2936 2932,2933,2934,2935,2936,2937,2938 2932,2933,2934,2935,2936,2937,2938,2939,2940 2932, 2933,2934,2935,2936,2937,2938,2939,2940,2941,2942

第二行:

2932,2933,2934,2935,2936,2937,2938,2939,2940,2941,2942,2943,2944,2945 2932,2933,2934,2935,2936,2937,2938,2939,2940,2941,2942,2943 ,2944,2945,2946,2947

最后一行:

2932,2933,2934,2935,2936,2937,2938,2939,2940,2941,2942,2943,2944,2945,2946,2947,2948,2949,2950,2951,2952,2953,2954,2955,2956,2957,2958,2959,2960,2961,2962,2963,2964,2965,2966,2967,2968,2969,2970

有谁知道如果在这样的实体框架构建,以避免这些重复?

2932 2932,2933 2932,2933,2934 2932,2933,2934,2935,2936 2932,2933,2934,2935,2936,2937,2938 2932,2933,2934,2935,2936,2937,2938,2939,2940 2932,2933,2934,2935,2936,2937,2938,2939,2940,2941,2942 The second line: 2932,2933,2934,2935,2936,2937,2938,2939,2940,2941,2942,2943,2944,2945 2932,2933,2934,2935,2936,2937,2938,2939,2940,2941,2942,2943,2944,2945,2946,2947 The last line: 2932,2933,2934,2935,2936,2937,2938,2939,2940,2941,2942,2943,2944,2945,2946,2947,2948,2949,2950,2951,2952,2953,2954,2955,2956,2957,2958,2959,2960,2961,2962,2963,2964,2965,2966,2967,2968,2969,2970 Does anyone know if there is a build in way in Entity framework to avoid these duplicates?

推荐答案

老实说,我还没有你要找的内容完全清楚。然而,有一对夫妇的事情,我可以根据您的扩​​展问题再说。首先,当你拉的任何的从数据库中,有没有内在顺序。默认情况下,它通常会被PK排序,如果可能的话,或者更准确的插入命令。然而,这是有风险的依靠,如果一个确切的秩序是必要的。如果你需要一个真正的精确和复制,能够顺序,那么你需要发出一个 ORDER BY 与你想要的顺序条款。

Honestly, I'm still not entirely clear on what you're looking for. However, there's a couple of things I can say based on your expanded question. First, when you pull anything from a database, there's no inherent order. By default, it'll generally be ordered by PK, if possible, or more appropriately by "insert order". However, that's risky to rely on if an exact order is necessary. If you need a truly exact and replicate-able order, then you need to issue an ORDER BY clause with the order you want.

特别是如果你依靠或者通过热切或延迟加载外键由实体框架充满导航属性,你不能在所有的的依赖隐秩序的上。同样,如果顺序很重要,那么你需要使用排序依据 OrderByDescending 有一些财产的实体,以确保你得到真正的苹果对苹果的订单比较。

Especially if you relying on navigation properties filled by Entity Framework through either eager or lazy-loading a foreign key, you can't rely on the implict order at all. Again, if order is important, then you need to use OrderBy or OrderByDescending with some property on the entity to make sure that you get a true apples-to-apples order comparison.

这篇关于获得&QUOT;&的indexOf QUOT;在实体框架的ICollection的中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆