score:11
if i understand you correctly, you're wondering, how linq provider can set properties of anonymous object, since they are "true" read only properties (there's no any private set
, but get
only)?
when you call select
extension method for iqueryable<t>
, it accepts an expression of type expression<func<t, tresult>
. if you'll write some stub for select
you can look into the generated expression, using debugger:
public static class myextensions
{
public static void myselect<t, tresult>(this iqueryable<t> query, expression<func<t, tresult>> projection)
{
system.diagnostics.debug.writeline(projection);
}
}
the difference is in how compiler generates lambda expressions for named types and anonymous types. when you call select
for named type, expression will look like this:
{_ => new person() {id = _.id, name = _.name}}
that is, firstly new person
object will be constructed, and then members will be initialized (memberinit
expression).
but when you call select
for anonymous type, expression will be built as constructor call (new
expression):
{_ => new <>f__anonymoustype0`2(a = _.id, b = _.name)}
linq provider compiles these lambdas into delegates, when materializing query results, and ultimately just calls constructor for anonymous type.
score:2
i found following difference in with anonymous type result of a linq result.
result is not edit able e.g. if we assign value to a gridview it will be read only.
problem with scope of an anonymous object.we can't pass the type to another method. define a parameter of type var; var must always be followed by an initialization expression.
if you need result only in current context for read only purpose use anonymous query . if you need result in other function the you have to define type of object. the object type after new
will be created with properties you want to get from result define then in curly braces {}
.
it is not necessary to initialize all value of model class.
score:17
the error you're getting really doesn't have anything to do with linq. you can see the same thing without using linq at all:
var anonymous = new { name = "fred" };
anonymous.name = "joe"; // error, as properties of anonymous types are read-only
so if you want to modify the objects fetched by your linq query, you shouldn't use anonymous types. but both linq queries are statically bound - anonymous types are still fully known at compile-time, and the compiler applies normal type restrictions to them. for example:
var anonymous = new { name = "fred" };
console.writeline(anonymous.foo); // error: no property foo
int bar = anonymous.name; // error: no conversion from string to int
Source: stackoverflow.com
Related Query
- LINQ select query with Anonymous type and user Defined type
- LINQ query with GROUP BY and Count(*) into Anonymous Type
- Anonymous Type with Linq and Guid
- When selecting an anonymous type with LINQ from EF, is there no way to run a method on an object as you select it?
- Avoid extra loop and could not find implementation of query pattern for source type int Select not found
- User Defined Table Type not working with Linq
- How to create an anonymous type within linq query with TypeScript
- How to Select top (5) contributors group by Business type code in c# linq query
- Select values in anonymous type with func in linq select statement
- LINQ with sub select query using sum and group by?
- Select Distinct in Linq with anonymous type
- Conditional Group and Select in LINQ with type conversion
- LINQ query with SELECT and two GROUP-BY condition
- Linq query for a nested select statement with grouping and distinct
- How to Call SQLite User Defined Function with C# LINQ Query
- Why is this linq query with anonymous type faster than anything else I try?
- use linq to group by and then run select query with where condition
- Linq nested select query and result has same structure with selected items
- Select parent and children when type is the same with linq
- returning specified type for a linq query with anonymous type
- How to run linq query with select when a primary key column returns null and thus the related attribute is null
- LINQ and sql query with left join and select max subquery
- How do I build a query to select master and last detail (max(Id)) with LiNQ
- linq and json, anonymous type cannot have multiple properties with the same name
- How can I check null and assign new value in linq anonymous type to select expected sql result
- C# LINQ Query with Calculated Field and Subquery in a Select Clause
- Linq query with Groupby Select and foreach loop
- LINQ GroupBy anonymous type and select timestamp that is within +/- one minute
- How to call an Sql User defined Function using Entity frame work Code first approach with LInq c#
- string parsing in linq query and use anonymous type
More Query from same tag
- Building 'flat' rather than 'tree' LINQ expressions
- calculate user stats with LINQ
- LINQ IQueryable returning same rows with skip and take
- Manage null with LINQ
- Linq ToList/ToArray/ToDictionary performance
- No generic method 'ThenBy' on type 'System.Linq.Queryable'
- Fetching data from more than 2 tables using EntityFramework
- Operator '!' cannot be applied to operand of type 'method group'
- How to select all parent objects into DataContext using single LINQ query?
- What does "A new expression requires (), [], or {} after type" mean?
- Linq query is not populating Child Collection
- How can I count all items in multi-level list in linq
- Get the Latest date object in List of Tuples objects using Linq C#
- BLToolkit equivalent of LoadWith from L2S
- Simple C# foreach to LINQ question
- Convert LambdaExpression to String, Including Values
- C# Determine Duplicate in List
- Complex Object mapping using Automapper
- Convert regex matches to the list of strings
- How can I query different tables dynamically with LINQ?
- Include all fields after grouping and summing using c# linq
- Linq query to get data from a specific RavenDB collection not working as expected
- LINQ order outer list by property of inner list
- LINQ Statement foreach hour in day issue
- How to select values and key in a Dictionary of lists?
- Can I compress an if statement using linq in C#?
- How to create a json from database using linq
- Take pages and combine to list of pages
- LINQ DateTime Query that ignores milliseconds
- EF doing prefilter table by property/type