score:3
the var
keyword is irrelevant. you can do it like this:
switch(flag)
{
case 1: {
var query=from w in db.sometable
select w;
}
break;
case 2: {
var query=from w in db.sometable
where w.id==someid
select w;
}
break;
default:
break;
}
it is correct syntax, but i would suggest you to extract each case to each method instead.
score:0
if it is same table , you can try this:
var query=from w in db.sometable select w;
switch(flag)
{
case 1:
break;
case 2:
query=query.where(w.id==someid);
break;
default:
query=null; //since linq is delay query, if you don't use the data in query, it will do nothing.
break;
}
score:5
declaring with var
lets you shorten the code, but the variable that you declare remains statically typed, and the scope of that variable does not change.
if you need to use a variable outside switch
, declare it before the switch
statement, like this:
iqueryable<sometype> query = null;
switch (...) {
case 1: query = ...; break;
...
default: ...
}
now you can use query
outside the switch
.
note: there are cases where you must use var
because the type that you assign to it has no name, but in your first case the type has a name, so you do not need to use var
.
edit : your second case, however, does require a var
, because you are selecting an anonymous type. in situations like that there are several ways around this problem:
- you can declare a named type for the "superset" of columns that you select (i.e.
timetm
,temperature
, andradiation
), or - in .net 4.0 you can use
iqueryable<dynamic>
. this shifts some of compile-time checking into runtime, but if you have to go this route, it is very convenient.
Source: stackoverflow.com
Related Query
- How to reuse a variable declared by "var" in C#
- How to reuse a linq expression for 'Where' when using multiple source tables
- How can i compare the variable var with the enum in C#
- How to assign multiple LINQ Include() statements to a variable for code re-use?
- How to get code behind variable value into repeater itemtemplate
- How to reuse code inside a Expression<Func< ... >>?
- How to assign two different entities result set into single var variable using linq to sql c#
- How to assign LINQ Query to a variable and then use it later in the code
- How to assign the same var type variable to different linq to sql result sets?
- How do I find the text within a div in the source of a web page using C#
- How are people unit testing code that uses Linq to SQL
- What does this C# code with an "arrow" mean and how is it called?
- How to resolve Value cannot be null. Parameter name: source in linq?
- Building a LINQ expression tree: how to get variable in scope
- How can I reuse expressions within LINQ statements?
- How to count the number of code lines in a C# solution, without comments and empty lines, and other redundant stuff, etc?
- How to get specific element Count in XML or XElement variable
- How to "zip" or "rotate" a variable number of lists?
- Accessing Results View from variable in code
- How do you index into a var in LINQ?
- How Do I Create an Expression<Func<>> with Type Parameters from a Type Variable
- How to declare a Linq Expression variable in order to have it processed as a dbParameter
- How do I specify the type of the range variable in a LINQ query?
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- Variable 'x' is declared but never used error
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- How to pass LinQ Expressions from F# to C# code
- A local variable cannot be declared in this scope[Linq/Lambda expression]
- How to convert var to string[]
- How to create a delegate to a target with variable number of arguments
More Query from same tag
- getting nested results from linq to sql
- How to order items within the group of anonymous type?
- Comparing two ObservableCollection(s) to see if they are different
- Like SQL Query in LINQ
- C# Linq: Can you merge DataContexts?
- How to Implement search method for db records with multiple filters using lambda expressions
- How to sort MMMYY on Chart.Js and display the values that match the month?
- Building OrderBy-expression at runtime by property-name which can be nested
- Joining Linq Expressions
- Linq in large lists
- Complex LINQ 'Any' query
- Windbg Linq for managed objects
- How do I use Paging on a List<T>
- No parameterless constructor defined for this object ASP.NET Website
- How can I modify this C# code so that Visual Studio recognizes that I'm not an idiot?
- How to return arrays with the biggest elements in C#?
- OrderBy using WHERE in LINQ
- filtering out null values while converting datatable to dictionary using linq
- Better way of making a Full Outer Join using LINQ for two Queries? C#
- Shortest way to modify some element in list when condition is matched C#
- Linq Expression EF core
- Dynamic where clause to filter collection elements
- How do I use LINQ to select from DataTable, with a list of the required column names
- Add empty element to list
- Linq Outer Join with defaults on both sides
- Retrieve a list B from a list A
- return int[] not IEnumerable<int[]> in LinqToXml
- Why do asynchronous calls in C# need to be declared as such if the method they reside in is declared with the 'async' keyword already?
- What means Distinct in LINQ?
- Reduce foreach loops into a linq query