从EntityCollection中检索单个值的最快方法

时间:2019-01-15 23:49:06

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

我有一个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分钟限制,并认为我可以通过减少查找时间来减少花费的时间。

1 个答案:

答案 0 :(得分:0)

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

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;