Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Sonar Cube Smell issue for Linq

Posted on by 475

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

  • dkrishna Profile Picture
    dkrishna 475 on at
    RE: Sonar Cube Smell issue for Linq

    Thanks Ravi !

  • Verified answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Sonar Cube Smell issue for Linq

    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

  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Sonar Cube Smell issue for Linq

    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();


  • dkrishna Profile Picture
    dkrishna 475 on at
    RE: Sonar Cube Smell issue for Linq

    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 .

  • dkrishna Profile Picture
    dkrishna 475 on at
    RE: Sonar Cube Smell issue for Linq

    Thanks alot Ravi ..It works !!

  • Verified answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Sonar Cube Smell issue for Linq

    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
    dkrishna 475 on at
    RE: Sonar Cube Smell issue for Linq
                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;
    


               

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Sonar Cube Smell issue for Linq

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

  • dkrishna Profile Picture
    dkrishna 475 on at
    RE: Sonar Cube Smell issue for Linq

    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

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Sonar Cube Smell issue for Linq

    Hi,

    You can simply replace Where with FirstOrDefault.

    Hope this helps.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans