none
How to filter a query select depending on values from a form? RRS feed

  • Question

  • Hi;
    How can I "input" parameters depending on form fields values?

    My form has DateStart, DateEnd and Message fields, all optional and I need to pass the values if they were set, I'm asking the how to for the query itself:

     

          var logList = (from ldm in qasEntities.LogsDataManipulations
                  join u in qasEntities.Users on ldm.Users_ID equals u.ID
                  select new
                  {
                    UserId = u.ID,
                    UserName = u.LastName + " " + u.FirstName,
                    LogDateTime = ldm.DateTime,
                    LogMessage = ldm.Message
                  }).OrderByDescending(x => x.LogDateTime);
    
    

     


    Must I create a specific query for each possible condition, like:

     

    if (DateStart is set){ query with datestart filter }
    elseif (DateEnd is set){ query with dateend filter }
    elseif (Message is set){ query with message filter }
    elseif (DateStart AND DateEnd is set){ query with datestart and dateend filter }
    ..
    else{ global query, no filter }
    
    Thanks
    Sunday, July 24, 2011 3:13 PM

Answers

  • Hi,

    You can embed all these in your query.

    First make DateStart and DateEnd as nullable, I already expect that Message is a string type?

    You can then do something like this:

    where (DateStart != null && DateStart <= <datecolumn>) && (DateEnd != null && DateEnd >= <datecolumn>) && (string.IsNullOrEmpty(Message) == false && Message == <messagecolumn>)

    Of course, this may not be the actual filter you request, but you get the idea on how to do it.

    Hope this helps!


    --Rune

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Scal1050 Monday, July 25, 2011 8:24 AM
    Sunday, July 24, 2011 6:24 PM

All replies

  • Hi,

    You can embed all these in your query.

    First make DateStart and DateEnd as nullable, I already expect that Message is a string type?

    You can then do something like this:

    where (DateStart != null && DateStart <= <datecolumn>) && (DateEnd != null && DateEnd >= <datecolumn>) && (string.IsNullOrEmpty(Message) == false && Message == <messagecolumn>)

    Of course, this may not be the actual filter you request, but you get the idea on how to do it.

    Hope this helps!


    --Rune

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Scal1050 Monday, July 25, 2011 8:24 AM
    Sunday, July 24, 2011 6:24 PM
  • Thanks Rune!

    Monday, July 25, 2011 8:24 AM