Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How I can check for null parameter in linq query for CRM

(0) ShareShare
ReportReport
Posted on by 10

I am using linq to query data from CRM, I want to check the conditions that have values and ignore the others , I used the following code:

return (from c in CTX.CreateQuery("trainingcourse")
    where CourseID == null || (Guid)c["trainingcourseid"] == CourseID
       && FromDate == null || (DateTime)c["startdate"] <= FromDate
       && ToDate == null || (DateTime)c["startdate"] >= ToDate
       && SearchText == null || ((string)c["title"]).Contains(SearchText) ||          
                                ((string)c["description"]).Contains(SearchText)
    select c)

But this cause the following application error

Invalid 'where' condition. An entity member is invoking an invalid property or method.

*This post is locked for comments

  • Suggested answer
    KZee Profile Picture
    KZee on at
    RE: How I can check for null parameter in linq query for CRM

    LINQ is converted to FetchXml and there is no way in FetchXml to deal with your local variable on its own. You will get an error even if you try SearchText.Length > 0 or SearchText != "" or string.IsNullOrEmpty(SearchText).

    Your code is good in terms of C# but not according to the engine converting your LINQ to FetchXml because of the way it works.

    Following code works just fine because there it will not be converted to FetchXml.

    var list = new List<string>();

    (from s in list

    where SearchText == null || s == SearchText

    select s).Dump();

    You can either create many if statements to first check your local variables for null to create different LINQ or the easiest would be to use QueryExpression which allows you to add conditions and you can add a condition if your variable is not null.

    msdn.microsoft.com/.../gg334688.aspx

  • Prutal Profile Picture
    Prutal 10 on at
    RE: How I can check for null parameter in linq query for CRM

    well yes the problem is because of the local variable, I want to check for the "trainingcourseid" only if "CourseID" is not null

    in entity framework

    i use something like this  

    CourseID is a parameter passed optionally

    CTX.Table.Where(x=>CourseID ==null || x.CourseID==CourseID).ToList();

    it suppose to work like an optional parameter for a method.

    any suggestion how to achieve this.

  • Suggested answer
    KZee Profile Picture
    KZee on at
    RE: How I can check for null parameter in linq query for CRM

    You are getting error message because are checking local variables for null in your LINQ query.

    e.g. Following code works

    (from c in serviceContext.CreateQuery("incident")

       where ((string)c["title"]).Contains(SearchText) || ((string)c["description"]).Contains(SearchText)

       select c

    )

    But if you introduce a null check for your local variable then you would get that error.

    (from c in serviceContext.CreateQuery("incident")

       where SearchText == null || ((string)c["title"]).Contains(SearchText) || ((string)c["description"]).Contains(SearchText)

       select c

    )

    Following link explains how to use Contains in LINQ

    msdn.microsoft.com/.../gg334415.aspx

    I hope this helps.

  • Suggested answer
    Daniel Wikell Profile Picture
    Daniel Wikell 2,360 on at
    RE: How I can check for null parameter in linq query for CRM

    The error message you receive is because you are using .Contains which is not a supported feature in the Linq->fetchxml engine.

    See this article for which LINQ-features are supported: msdn.microsoft.com/.../gg328328.aspx

    If you want to use other LINQ-functions you will first have to convert the result to a regular list and then apply the contains criteria afterwards.

    Also LINQ-fetchxml where-criterias are limited to the form of <property> <operator> <value> meaning that if you want to check if the trainingcourseid property is null you should check against that property on the left hand side and null on the right hand side. If you want to check if the local variable CourseID is null you should do it outside of the LINQ-query.

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey Pt 2

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,514 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans