0

Here's my code:

var query = from x in Data

select new { Fruit = x[0], Animal, x[2], Color = x[3], Food = x[4] };

Now, I want to create checkboxes such that if a user checks off "box 1" it will do a different query like so:

select new { Fruit = x[7], Animal, x[4], Color = x[8], Food = x[9] };

My question is, how do I make it so that depending on which checkbox is checked a different select...new statement will be used?

I think I can use multiple if/then statements to see which particular box is checked and to determine which select...new statement to use, but I'm guessing there is a better way out there.

In VB I think a "case" statement would come into play, but I don't know what is the C# equivalent.

My failed attempt at implementing case/switch in the context of changing query parameters:

int caseSwitch = 1;
var query = from x in Data

switch (caseSwitch)
{

       case 1:
           select new { Fruit = x[0], Animal, x[2], Color = x[3], Food = x[4] };
           break;

       case 2:
           select new { Fruit = x[7], Animal, x[4], Color = x[8], Food = x[9] };
           break;
}
10
  • 2
    the C# equivalent is the switch statement Commented Oct 4, 2013 at 21:06
  • Ok I see switch/case..but how do I implement it in the context of a query? I tried putting switch(caseSwitch) { case 1: <insert select new code>; break; }, but I don't think I'm doing it right. Commented Oct 4, 2013 at 21:13
  • 1
    Why not put the whole query inside the switch statement? Commented Oct 4, 2013 at 21:17
  • 1
    isn't there a formula you could use to calculate the indexes of the array you want based on the input of the checkboxes? Commented Oct 4, 2013 at 21:19
  • 2
    Whats the logic behind determining your indexes? You could reduce the duplicate code if there is logic for determining the indexes instead of just having them hardcoded for each situation... Commented Oct 4, 2013 at 21:29

2 Answers 2

1

In order to vary your query you can use a switch statement. However, in doing so you cannot use anonymous types. You will need to define a object that you can use, so the query object can be defined when declared.

public class Foo
{
    public string Fruit { get; set; }
    public string Animal { get; set; }
    public string Color { get; set; }
    public string Food { get; set; }
}

IEnumerable<Foo> query = null;
switch (caseSwitch)
{
    case 1:
        query = from x in Data
                select new Foo { Fruit = x[0], Animal = x[2], Color = x[3], 
                    Food = x[4] };
         break;

     case 2:
         query = from x in Data
                select new Foo { Fruit = x[7], Animal = x[4], Color = x[8],
                    Food = x[9] };
         break;

     default:
         // handle however you need to
}

You can also inline it entirely into your LINQ query, however, if you extending the code for multiple cases will make the code more difficult to understand and maintain.

var query = from x in Data
            // needed to get the caseSwitch value into context in the select
            //  otherwise it is considered out of context
            let sw = caseSwitch  
            select sw == 1 ?
                new { Fruit = x[0], Animal = x[2], Color = x[3], Food = x[4] } :
                new { Fruit = x[7], Animal = x[4], Color = x[8], Food = x[9] }

The issue with this approach is when the caseSwitch is outside the valid range of values you may get a value you did not want. This is better handled by using a switch statement, where you can set the query to a default value or throw an exception when the default case is reached.

Example of how the inlined LINQ with more than two cases

var query = from x in Data
            let sw = caseSwitch  
            select 
                sw == 1 ? new { Fruit = x[0], Animal = x[2], Color = x[3], Food = x[4] }
                : sw == 2 ? new { Fruit = x[7], Animal = x[4], Color = x[8], Food = x[9] }
                : sw == 3 ? new { Fruit = x[10], Animal = x[11], Color = x[12], Food = x[13] }
                : null; // final value is the default value
Sign up to request clarification or add additional context in comments.

Comments

1

You could factor out the select item into a function:

function object getItem(DataItem x, int caseSwitch)
{
    switch (caseSwitch)
    {
       case 1:
           return new { Fruit = x[0], Animal, x[2], Color = x[3], Food = x[4] };
           break;
       case 2:
           return new { Fruit = x[7], Animal, x[4], Color = x[8], Food = x[9] };
           break;
    }
}

And then you can do the following query:

int caseSwitch = 1;
var query = from x in Data
            select getItem(x, caseSwitch);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.