none
Linq to Brainlock RRS feed

  • Question

  •  

    I'm using Linq to populate charts and make pretty pictures of bonds as they flow around the world. All was well until I added some query filter options to the UI, and then I drew a blank.

     

    So basically I've got the "main" query params which won't change -- let's say that it pulls a date and some other numbers from the record no matter what. I've also got a couple other fields in this table that the user may wish to filter out or may wish to ignore.

     

    I'm not sure how to go about writing Linqspeak that includes this kind of conditional thinking in its WHERE clause... something like:

     

    var v =

    from p in whatever

    where p.date == date && // always

    p.rate == rate && // always

    //this is the tricky bit

    if (myOptions.type == primary)

    p.type == "primary"

    else if( myOptions.type == secondary)

    p.type == "secondary"

    else

    ignore p.type altogether

     

    I know this is gibberish but I think it conveys my intention.

     

    Obviously if this were SQL I'd just build the string. And maybe I should just do that, but I figured there's probably a way to do this with Linq that I just don't know about. I'm still picking it up as I go.

     

     

    Monday, April 28, 2008 9:25 PM

Answers

  • Linq queries are composable, and you can use that to solve your problem:

     

    // get the part of the query that is always there

    var query =

    from p in whatever

    where p.date == date && // always

    p.rate == rate // always

    select p;

     

    // compose on the top if needed

    if (myOptions.type == primary)

    {

    query = from p in query

      where p.type == "primary"

      select p;

    }

    else if( myOptions.type == secondary)

    {

    query = from p in query

      where p.type == "secondary"

      select p;

    }

     

     

    Hope that helps,

    Maurycy

     

    Tuesday, April 29, 2008 12:20 AM

All replies

  • Linq queries are composable, and you can use that to solve your problem:

     

    // get the part of the query that is always there

    var query =

    from p in whatever

    where p.date == date && // always

    p.rate == rate // always

    select p;

     

    // compose on the top if needed

    if (myOptions.type == primary)

    {

    query = from p in query

      where p.type == "primary"

      select p;

    }

    else if( myOptions.type == secondary)

    {

    query = from p in query

      where p.type == "secondary"

      select p;

    }

     

     

    Hope that helps,

    Maurycy

     

    Tuesday, April 29, 2008 12:20 AM
  • Clever. I had no idea you could build it in pieces like that.

     

    Thanks, Maurycy.

     

    ** edit

     

    Did I say "clever"? Actually that's pretty awesome.

    Tuesday, April 29, 2008 1:18 PM
  • Seems old post.. but another way of doing this could be:

      var v = from p in whatever  
                        where p.date == date && // always  
                        p.rate == rate && // always  
                        //do check in function  
                        checkMyOptions(myOptions, p)  
                            select p;  
     
      private bool checkMyOptions(MyOptions myOptions, WhateverItem p)  
            {  
                if (myOptions.type == primary)  
                    return p.type == "primary";  
                else if (myOptions.type == secondary)  
                    return p.type == "secondary";  
                else  //nothing to match return TRUE to exclude this condition
                    return true;  
            }  
     
    Tuesday, December 23, 2008 7:11 PM