I keep running into a pattern whereby I want to select rows from an Entity Collection (EF4) and use the data to create new rows in a different entity collection.
The only way I have found to do this is to perform the following steps:
var newEntities = (from e in myentities
where e.x == y
select new {
Prop1 = e.Prop1,
Prop2 = e.Prop2+e.Prop3,
Prop3 = DateTime.Now,
Prop4 = "Hardcodedstring"}
)
.AsEnumerable()
.Select(n=>new OtherEntity{
Prop1 = n.Prop1,
Prop2 = n.Prop2,
Prop3 = n.Prop3,
Prop4 = n.Prop4}
);
//insert into database and save
If I try to create a new OtherEntity in the select then I get an EF exception.
Is this the only way to proceed? Makes the whole thing very cumbersome and seems a complete waste of keystrokes?
I suggest using Automapper to map from your entities to your domain objects instead of doing the mapping from within the linq query.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With