简体   繁体   English

在Dynamics CRM 2011 Linq查询中进行双重订购

[英]double order by in Dynamics CRM 2011 Linq query

I'm developing a survey module for CRM 2011, where a CRM user can make a survey with multiple questions of different types and ask contacts to answer them using a dynamic web page on Azure. 我正在开发CRM 2011的调查模块,其中CRM用户可以使用多个不同类型的问题进行调查,并要求联系人使用Azure上的动态网页来回答。 I have completed about 90% of it so far and just need to add a few minor changes involving data management. 到目前为止,我已经完成了大约90%的工作,只需要添加一些涉及数据管理的小更改即可。

The part I'm currently optimizing involves sorting my questions. 我当前正在优化的部分涉及对我的问题进行排序。 I have 2 entities in CRM: surveyquestion and question. 我在CRM中有2个实体:Surveyquestion和Question。 question refers to an actual question in the survey, while surveyquestion is an NN relationship entity with a few additional fields, so we can reuse questions in different surveys. 问题是指调查中的实际问题,而Surveyquestion是具有几个其他字段的NN关系实体,因此我们可以在不同调查中重用问题。 I have a field "groep" in "question" which is used to indicate whether a question should be grouped with another question on the form. 我在“问题”中有一个字段“ groep”,该字段用于指示是否应将一个问题与表格上的另一个问题组合在一起。 I have another field, "volgorde" in "surveyquestion", which is used to indicate the order of the questions. 我在“调查问题”中还有一个字段“ volgorde”,用于指示问题的顺序。 I wish to show all questions with the same "group" together, and then order any questions inside groups (and ungrouped questions as a separate group) by the sortorder field. 我希望将所有具有相同“组”的问题一起显示,然后按排序顺序对组内的所有问题(以及未分组的问题作为单独的组)进行排序。 So I basically need to order on 2 fields in 2 different tables, which seems to be quite tricky. 因此,我基本上需要对2个不同表中的2个字段进行排序,这似乎很棘手。

This is the code I'm currently using. 这是我当前正在使用的代码。 I'm getting a nullreferenceexception on the join for vraaglist, which is weird because both vr.Id and ev.slfn_vraag.Id have data in the relevant rows. 我在vraaglist的连接上收到nullreferenceexception,这很奇怪,因为vr.Id和ev.slfn_vraag.Id都在相关行中有数据。 I don't think it's due to the orderby, because the commented code above works (but I cannot simply add ev.volgorde to my orderby because he complains about that. 我不认为这是由于orderby引起的,因为上面的注释代码有效(但是我不能简单地将ev.volgorde添加到我的orderby中,因为他对此有所抱怨。

public List<slfn_vraag> GetVragenforEnquete(Guid enGuid)
    {
        //List<slfn_vraag> vraaglist = (from vr in _oContext.slfn_vraagSet
        //                              join ev in _oContext.slfn_enquetevraagSet on vr.Id equals ev.slfn_vraag.Id
        //                              orderby vr.slfn_Groep
        //                              where ev.slfn_enquete.Id == enGuid
        //                              select vr).ToList();

        IQueryable<slfn_enquetevraag> enquetevraaglist = (from ev in _oContext.slfn_enquetevraagSet
                                       orderby ev.slfn_volgorde
                                       where ev.slfn_enquete.Id == enGuid
                                       select ev);
        List<slfn_vraag> vraaglist = (from vr in _oContext.slfn_vraagSet
                                      join ev in enquetevraaglist on vr.Id equals ev.slfn_vraag.Id
                                      orderby vr.slfn_Groep
                                      select vr).ToList();
        return vraaglist;
    }

The code compiles without errors, but when i run it, I get a NullReferenceException on the question query. 该代码编译没有错误,但是当我运行它时,我在问题查询中得到了NullReferenceException。 does anyone have any ideas on how to fix this? 有没有人对如何解决这个问题有任何想法?

edit: exception details from VS2012 as requested below. 编辑:VS2012的异常详细信息,如下所示。

  System.NullReferenceException occurred
  HResult=-2147467261
  Message=De objectverwijzing is niet op een exemplaar van een object ingesteld.
  Source=Microsoft.Xrm.Sdk
  StackTrace:
       bij Microsoft.Xrm.Sdk.Linq.QueryProvider.TranslateJoin(QueryExpression qe, IList`1 methods, Int32& i, Projection& projection, List`1& linkLookups)
       bij Microsoft.Xrm.Sdk.Linq.QueryProvider.GetQueryExpression(Expression expression, Boolean& throwIfSequenceIsEmpty, Boolean& throwIfSequenceNotSingle, Projection& projection, NavigationSource& source, List`1& linkLookups)
       bij Microsoft.Xrm.Sdk.Linq.QueryProvider.Execute[TElement](Expression expression)
       bij Microsoft.Xrm.Sdk.Linq.QueryProvider.GetEnumerator[TElement](Expression expression)
       bij Microsoft.Xrm.Sdk.Linq.Query`1.GetEnumerator()
       bij System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       bij System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       bij Enquete_Webform.Data.EnqueteGenerator.GetVragenforEnquete(Guid enGuid) in e:\VS\tfs_products\MS CRM\2011\Enquete\Enquete_Webform\Enquete_Webform\Data\EnqueteGenerator.cs:regel 49
  InnerException: 

Enumerable.OrderByEnumerable.ThenBy应用于最终的vraaglist ,可能会为您的案例提供解决方案。

I'm guessing you have an ev.slfn_vraag that is null. 我猜你有一个ev.slfn_vraag为空。 Try adding a where statement to exclude those that have a null value 尝试添加where语句以排除那些具有空值的语句

List<slfn_vraag> vraaglist = (from vr in _oContext.slfn_vraagSet
                              join ev in enquetevraaglist on vr.Id equals ev.slfn_vraag.Id
                              where ev.slfn_vragg.Id != null
                              orderby vr.slfn_Groep
                              select vr).ToList();

You could also have the null in the first statement as well: 您也可以在第一条语句中使用null:

var enquetevraaglist = (from ev in _oContext.slfn_enquetevraagSet
                        orderby ev.slfn_volgorde
                        where ev.slfn_enquete != null && ev.slfn_enquete.Id == enGuid
                        select ev)

I've created an extension method on the Entity Reference class that allows for nulls without erroring. 我已经在Entity Reference类上创建了一个扩展方法,该方法允许没有错误的空值。

    /// <summary>
    /// Returns the Id of the entity reference or Guid.Empty if it is null"
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public static Guid GetIdOrDefault(this EntityReference entity)
    {
        if (entity == null)
        {
            return Guid.Empty;
        }
        else
        {
            return entity.Id;
        }
    }

And as far as your question about sorting, CRM doesn't support sorting by a linked field, just keep that in mind. 至于您有关排序的问题,CRM不支持按链接字段进行排序,请记住这一点。

After reading Daryl's comment about FetchXML, I've decided to convert my LINQ query to FetchXML, because this does support linked entity sorting. 阅读了Daryl关于FetchXML的评论后,我决定将LINQ查询转换为FetchXML,因为它确实支持链接实体排序。 It now works as it should, and I get the results I need. 现在它可以正常工作了,我得到了所需的结果。

for completion, this is the fetchXML I ended up using: 为了完成,这是我最终使用的fetchXML:

<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>
  <entity name='slfn_vraag'>
   <attribute name='slfn_vraagid' />
   <attribute name='slfn_vraag' />
   <attribute name='slfn_typevraag' />
   <attribute name='slfn_groep' />
   <order attribute='slfn_groep' descending='false' />
   <link-entity name='slfn_enquetevraag' from='slfn_vraag' to='slfn_vraagid' alias='aa'>
       <attribute name='slfn_volgorde'/>
       <order attribute='slfn_volgorde' descending='false' />
       <filter type='and'>
           <condition attribute='slfn_enquete' operator='eq' value='" + enGuid+@"' />
       </filter>
   </link-entity>
  </entity>
</fetch>

As an aside, None of the fields could be empty, because they were all primary and foreign keys in CRM, linking to other entities. 顺便说一句,这些字段都不能为空,因为它们都是CRM中链接到其他实体的主键和外键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM