score:2
Yes, you can declare a function, but you can't define it to return a property of the anonymous type that is currently being declared-- if you were to use this
, for example, the lambda would capture the value of this
in the declaring context (the class that contains the code that is executing the LINQ). You can however capture the values that are being used to initialize the anonymous class (as I do in the example below).
Note that this is not the same as giving the anonymous class a method. Anonymous classes can only have read-only, immutable properties. In this case, your anonymous class will still have the original ToString()
method inherited from object
. This may make it tricky to distinguish the function held in the ToString
property from the ToString()
method, so maybe you should use a different name.
Also, you forgot the new
keyword before Func
.
var personsByCity = from p in people join c in cities on p.CityId equals c.Id
select new
{
PersonName = p.Name,
CityName = c.Name,
ToString = new Func<string>( () => {
return c.Name + "/" + p.Name;
})
};
Source: stackoverflow.com
Related Articles
- Can I declare a function inside a LINQ select clause?
- C# if statment inside LINQ select clause
- What is the difference between creating a new object inside select LINQ clause and inside a method
- Lambda function in LINQ select clause
- linq store select clause as function variable
- Conditions inside Select clause in Linq
- Lambda function with two input parameter in linq Select clause
- Linq code to select one item
- Using Linq select list inside list
- How to select last record in a LINQ GroupBy clause
- Syntax to execute code block inside Linq query?
- Difference between cast and as inside a select in LINQ
- Why C# LINQ expressions must end with Select or Group By Clause where as no such restriction in VB.Net
- How to create a dynamic LINQ select projection function from a string[] of names?
- LINQ select to new object, setting values of object in function
- Using Function in Select Clause of Entity Framework Query
- SELECT AS in Linq with WHERE clause
- How can I call a local function inside a LINQ query?
- IF statement inside a LINQ SELECT to include columns
- Calling a custom scalar DB function inside a LINQ to Entities query?
- How to Use Effeciently Where Clause or Select in LINQ Parallel in Large Dataset
- LINQ Source Code Available
- Linq with where clause in many-to-many EF Code First object
- Method calls inside LINQ select (efficiency)
- Using async/await inside a Select linq query
- Creating a dynamic Linq select clause from Expressions
- C# linq Select objects in list looking inside another list of objects
- When Where clause is used inside Linq statement produces different results than when used outside
- LINQ to XML : A query body must end with a select clause or a group clause
- Is there a way to break out of a LINQ SELECT on completion of a custom WHERE Clause
- Improving performance of an Entity Framework linq query
- Linq and object's wrapper
- C# List, Removing all null values *after* last non-null element
- Linq :DataTable select does not work if column name has space in it?
- Filtering data from parameters c#
- How do I exclude a member from Linq-To-Sql mapping?
- LINQ - Select all in parent-child hierarchy
- linq to sql select specific cell
- How to remove spaces from linq data in C#
- Using Linq to query repository returning invalid results
- How does Linq know the name of an element?
- remove tuple from list using linq
- How to group and subgroup and get data from a linq query
- How to select the properties to be filled instead of all properties of the object
- Linq Anonymous Type
- Why IEnumerable<T> becomes empty after adding elements to a collection?
- Dynamic Link, changing a property for every item in a list
- .DBML file and LINQ to SQL
- EF Loading Filtered Child Entries only
- Creating a Custom Predicate using a String Value for the Numeric Operand