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

Announcements

No record found.

News and Announcements icon
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 485

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
    485 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
    485 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
    485 on at

    Thanks alot Ravi ..It works !!

  • dkrishna Profile Picture
    485 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
    485 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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
AS-17030037-0 Profile Picture

AS-17030037-0 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans