score:1
you could quite easily move the switch logic out into another function like so:
private static t getreturnvalue<t>(myclass x)
{
switch (x)
{
case 1:
return math.sqrt(x.field1);
break;
case 2:
return math.pow(x.field2,
2);
break;
default:
return x.field3;
break;
}
}
and then you just need to pass your object to that function to get back the value you want:
var q = from x in myanonymoustypecollection
select new
{
id = x.id,
calcfield = getreturnvalue(x)
};
score:9
you could wrap your anonymous function as a (self-executing) func<>
delegate. this assumes you know the return type.
var q = from x in myanonymoustypecollection
select new {
id = x.id,
calcfield = new func<double>( () => {
switch(x.somefield) {
case 1:
return math.sqrt(x.field1);
case 2:
return math.pow(x.field2, 2);
default:
return x.field3;
}
} )()
};
score:13
first off, i usually prefer the method chain syntax over the query syntax for linq. with that you can do this easily.
var q = myanonymoustypecollection
.select(x =>
{
object calcfield;
switch(x.somefield)
{
case 1:
calcfield = math.sqrt(x.field1);
case 2:
calcfield = math.pow(x.field2, 2);
default:
calcfield = x.field3;
return new
{
x.id,
calcfield = calcfield
};
});
without using method chains, you need either a method or an func. let's assume a func
//replace these with actual types if you can.
func<dynamic, dynamic> calculatefield =
x =>
{
switch(x.somefield) {
case 1:
return math.sqrt(x.field1);
case 2:
return math.pow(x.field2, 2);
default:
return x.field3;
}
var q = from x in myanonymoustypecollection
select new { x.id, calcfield = calculatefield(x) };
note: i didn't write this in an ide, so please excuse any simple errors.
here is the msdn for dynamic. however, i have found that once you need to start passing anonymous types around, it is best to make an actual class.
Source: stackoverflow.com
Related Query
- Syntax to execute code block inside Linq query?
- How to execute code as a part of the LINQ query
- LINQ - Query syntax vs method chains & lambda
- .NET LINQ query syntax vs method chain
- Calling a method inside a Linq query
- GroupBy with linq method syntax (not query syntax)
- LINQ Lambda vs Query Syntax Performance
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- How to Convert LINQ Comprehension Query Syntax to Method Syntax using Lambda
- AsNoTracking using LINQ Query syntax instead of Method syntax
- Syntax for linq query to List<string>
- Linq query to filter id inside a list of list c#
- LINQ - Method vs Query Syntax Difference
- LINQ Query Syntax to Lambda
- SQL Query to LINQ syntax using not exist and join
- Call class method inside the Linq Query
- Using Linq query inside List<T>.AddRange to conditionally add elements from one list to another
- Linq mix extension and query syntax
- MVC Razor brackets inside code block
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- C# multiple variables in lambda expression inside LinQ query
- Wrap a Linq query in a try/catch block using a method declaration
- Linq Query syntax vs. Method Chain: return type
- How does linq actually execute the code to retrieve data from the data source?
- Entity Framework: Update inside LINQ query
- Use dictionary inside linq query
- LINQ Source Code Available
- Call a method inside LINQ Query
- Convert EF LINQ method syntax to query syntax
- SQL ROW_NUMBER() in LINQ Query Syntax
More Query from same tag
- Use linq to filter by property of subcollection
- Group list of objects with corresponding child with multiple inheritance using LINQ
- Linq - how do I select several fields without using new
- Unable to translate SQL query to LINQ using EF
- Sorting a list based on another list or a different size
- grouping the list object with where clauses and return
- How to make string appear first in order by in linq statement
- Linq -- How to perform Join in ForEach statement?
- The type arguments cannot be inferred from the query
- Get a list from a dynamic json result
- Which conditions must a query meet so that it can be translated to the SQL server?
- Adding Sum per row in PIVOT using LINQ
- How to select multiple elements into array in Linq query?
- System.Collections.Generic.List`1[System.String] instead of data
- Show latest three items on index page from database
- Can 2d array be converted to usable list
- Entity Framework 6 Compiled LINQ Query
- remove tuple from list using linq
- How to map two expressions of differing types?
- How to grab letters from a word multiple times with C#/LINQ
- Counting occurrences of the first x elements of sublist in all sublists of the jagged list without grouping
- InvalidCastException error with linq
- How to create lambda expression that returns object's property, having this property's name?
- How to add in Linq to EF data in list form to Class Object C#?
- How to find point closest to 0,0 point with LINQ and C#
- The right way to use lambda expressions in linq
- Using already computed element
- Get from IQueryable<T> predicate Func<T, bool>
- Ordering XElements
- Order reversed in Aggregate LINQ query