Overhed
Overhed

Reputation: 1314

LINQPad LINQ To SQL Updates with Anonymous Types

Say I have the following query in LINQPad targeting a SQL DB (using C# Statement(s) mode):

var query = (from te in Time_Entries
            select new { te.Period, te.Company_Name }).FirstOrDefault();

If I wanted to update the Period value on the selected record, I would think that I could do something like:

query.Period = 5;
SubmitChanges();

But unfortunately, I get an error on the query.Period assignment line:

Property or indexer 'AnonymousType#1.Period' cannot be assigned to -- it is read only

Is it possible to perform an update this way or in a similar way?

Upvotes: 0

Views: 1322

Answers (2)

Pleun
Pleun

Reputation: 8920

Well, the answer is already in the

select new {}

Even if it would not be an anonymous type, all it could be is an insert....

The rest is answered by walther in his answer.

Upvotes: 1

walther
walther

Reputation: 13598

No, you can't. Anonymous types can't have properties that can be modified. From the documentation:

Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first.

It doesn't really make sense anyway. Anonymous types are sometimes very useful, but not when you need to use Linq2Sql entity tracking and updating...

Upvotes: 3

Related Questions