【问题标题】:Fastest way to retrieve single value from EntityCollection从 EntityCollection 检索单个值的最快方法
【发布时间】:2019-01-15 23:49:06
【问题描述】:

我有一个 Entitycollection,我将其转换为 List,如下所示:

var List = Lines.Entities.Where(p =>
                    p.GetAttributeValue<OptionSetValue>("new_time").Value == 100000003).Select(e =>
                    new {

                        group = e.GetAttributeValue<OptionSetValue>("new_group"),
                        desc = e.GetAttributeValue<string>("new_desc"),
                        numbers = new Dictionary<string, int>()
                            {
                                {"monday", e.GetAttributeValue<int>("new_mondayunits") },
                                {"tuesday", e.GetAttributeValue<int>("new_tuesdayunits") },
                                {"wednesday", e.GetAttributeValue<int>("new_wednesdayunits")},
                                {"thursday", e.GetAttributeValue<int>("new_thursdayunits") },
                                {"friday", e.GetAttributeValue<int>("new_fridayunits") }
                            }

                    }).ToList();

我像这样在我想要的那一天检索值:

var value = List.Where(e => e.group == group && e.desc == desc).Select(e => e.numbers["monday"]);

我的目的是以最快的方式从 EntityCollection 中检索值,因为这些值是从 for 循环中调用的。我不确定将 entitycollection 转换为列表是否有帮助。这是实体集合:

                    string fetchContractLines = @"  
                        <fetch version='1.0' output-format='xml-platform' mapping='logical'>
                            <entity name='new_contractline'>
                                <attribute name='new_group' />
                                <attribute name='new_desc' />
                                <attribute name='new_time' />
                                <attribute name='new_mondayunits' />
                                <attribute name='new_tuesdayunits' />
                                <attribute name='new_wednesdayunits' />
                                <attribute name='new_thursdayunits' />
                                <attribute name='new_fridayunits' />
                                <filter type='and'>
                                    <condition attribute='statecode' operator='eq' value='0' />
                                    <condition attribute='new_contractid' operator='eq' value='{" + contractId.ToString() + @"}' />
                                    <condition attribute='new_group' operator='not-null' />
                                    <condition attribute='new_time' operator='not-null' />
                                    <condition attribute='new_desc' operator='not-null' />
                                </filter>
                            </entity>
                        </fetch>";


                    EntityCollection Lines = service.RetrieveMultiple(new FetchExpression(fetchContractLines));

我需要速度的原因是我达到了插件执行的 2 分钟限制,并认为我可以通过减少查找时间来减少它所花费的时间。

【问题讨论】:

  • FirstOrDefault() 做你想做的事吗?
  • 您是否希望每个组/描述对有一行 .Where(e =&gt; e.group == group &amp;&amp; e.desc == desc)?如果是这样,.Single() 将返回一行且仅返回一行,如果存在多于或少于一行,则抛出异常。
  • @Aron 取决于您的用例 singlefirstfirstordefault 慢很多,因为它遍历整个集合以确保这是一个“单一”匹配,而firstfirstordefault 将在找到匹配项后立即退出集合
  • 是的,我需要尽可能快的方法!!问题本质上是从该集合中获取单个整数值的最快方法是什么!我只期望一个值,但是,如果它不存在,那么我想要一个零来代替那个值!
  • 感谢@jasonscript 的提醒。说得通。看起来FirstOrDefault() 完全符合要求。

标签: c# linq dynamics-crm microsoft-dynamics dynamics-crm-online


【解决方案1】:

我改用了字典,但仍不确定是否有更快的方法从集合中检索值:

var Lookup = Lines.Entities.Where(e => e.GetAttributeValue<OptionSetValue>("new_time").Value == 100000001).Select(r => new
                    {
                        group = r.GetAttributeValue<OptionSetValue>("new_group").Value,
                        desc = r.GetAttributeValue<string>("new_desc"),
                        numbers = new Dictionary<string, int>()
                        {
                            {"monday", r.GetAttributeValue<int>("new_mondayunits") },
                            {"tuesday", r.GetAttributeValue<int>("new_tuesdayunits") },
                            {"wednesday", r.GetAttributeValue<int>("new_wednesdayunits") },
                            {"thursday", r.GetAttributeValue<int>("new_thursdayunits") },
                            {"friday", r.GetAttributeValue<int>("new_fridayunits") }
                        }
                    }).ToDictionary(t => t.group.ToString() + t.desc, t => t.numbers);
var quan = Lookup.ContainsKey(key) ? amLookup[key][currentday] : 0;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    • 1970-01-01
    • 1970-01-01
    • 2010-09-28
    • 2017-03-15
    • 1970-01-01
    • 2019-05-29
    相关资源
    最近更新 更多