web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Sonar Cube Smell issue for Linq

(0) ShareShare
ReportReport
Posted on by 483

Hello,

iam getting error as Drop 'Where' and move the condition into the 'FirstOrDefault'  for following linq code in Sonar Cube .. Please suggest how to fix and how can we move Where condition to FirstOrDefault

 Entity docLocation = entityCollection.Entities.Count > 0 ? entityCollection.Entities.Where(s =>
 

            parentLocationRecord.Id == (s.Contains("parentsiteorlocation") ? ((EntityReference)s["parentsiteorlocation"]).Id : Guid.Empty)
 

            && _recordId == (s.Contains("regardingobjectid") ? ((EntityReference)s["regardingobjectid"]).Id : Guid.Empty)).FirstOrDefault() : null;
 


*This post is locked for comments

I have the same question (0)
  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    You can simply replace Where with FirstOrDefault.

    Hope this helps.

  • dkrishna Profile Picture
    483 on at

    I tried but but getting error at FirstOrDefault in last line.

    Entity doesnt contain a defination for FirstOrDefault and No extension method 'FirstOrDefault'  accepting  first arugment type of Entity

  • RaviKashyap Profile Picture
    55,410 Moderator on at

    Can you share your code after updating it? I tried it and it seem to work.

  • dkrishna Profile Picture
    483 on at
                Entity docLocation = entityCollection.Entities.Count > 0 ? entityCollection.Entities.FirstOrDefault(s =>
                parentLocationRecord.Id == (s.Contains("parentsiteorlocation") ? ((EntityReference)s["parentsiteorlocation"]).Id : Guid.Empty)
                && _recordId == (s.Contains("regardingobjectid") ? ((EntityReference)s["regardingobjectid"]).Id : Guid.Empty)).FirstOrDefault() : null;
    


               

  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    remove the last FirstOrDefault.

    ============

    Entity docLocation = entityCollection.Entities.Count > 0 ? entityCollection.Entities.FirstOrDefault(s =>

              parentLocationRecord.Id == (s.Contains("parentsiteorlocation") ? ((EntityReference)s["parentsiteorlocation"]).Id : Guid.Empty)

              && _recordId == (s.Contains("regardingobjectid") ? ((EntityReference)s["regardingobjectid"]).Id : Guid.Empty)) : null;

    ============

    Hope this helps.

  • dkrishna Profile Picture
    483 on at

    Thanks alot Ravi ..It works !!

  • dkrishna Profile Picture
    483 on at

    Its working but does it effects any functionality as iam changing from Where to FirstOrDefault as FirstOrDefault will get first record or default record from list .

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi Krishna,

    Try with this - 

      entityCollection.Entities.Where(s => parentLocationRecord.Id == (s.Contains("parentsiteorlocation") ? ((EntityReference)s["parentsiteorlocation"]).Id : Guid.Empty) && _recordId == (s.Contains("regardingobjectid") ? ((EntityReference)s["regardingobjectid"]).Id : Guid.Empty)).FirstOrDefault();


  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    This will give you same results....with where...you were searching for records and then taking the first or default...with the uodated one you are still retrieving the first or defailt but here  you are passing the criteria with firstordefault....this is why you were getting warnings.

    Hope this helps

  • dkrishna Profile Picture
    483 on at

    Thanks Ravi !

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans