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

Community site session details

Session Id :
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

I have the same question (0)
  • Suggested answer
    Daniel Wikell Profile Picture
    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.

  • Suggested answer
    KZee Profile Picture
    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.

  • Prutal Profile Picture
    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
    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

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Community Member Profile Picture

Community Member 2

#1
UllrSki Profile Picture

UllrSki 2

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans