c# - How to change a value without new projection in LINQ? -


I have the following LINQ query:

  var Source = Select MyNods from the node New {id = node.id, name = node .name, painter id = node.parent id, // tap fit};   

In the above query, ParentId is the tap. Now I want a new result that matches the first one, but with small changes, if ParentId is zero, so I want it to be 0 .

I wrote it:

  var source2 = Select new from source {id = s.Id, name = s.Name, ParentId = s.ParentId ?? 0, // just change the zero values ​​to 0};   

Can I implement it in a simple way (I mean without new launches)?

Edit : The new projection is the same as the first one and both the ParentId is tap.

LINQ is not ideal to execute side effects on current storage. If you want to do this, you'll do better:

  foreach (var node in var node) {if (! Node.ParentId.HasValue) node.ParentId = 0; }   

If it is not, then you have to project. Your current query is perfectly fine; The only way I can think of shortening it is:

  var source2 = s new selection from source, s.Name, parent id = s.ParentId ?? 0}; Edit: It appears that you are trying to create an example of a different  type  (i.e. with virtually the same properties as the source, But non-measurable with a specific asset), so that you can avoid creating new types of examples and copying copies. You may want to consider writing a 'real' (non-anonymous) type that represents what you want and receives source-type to provide conversion-mode. Then you can do this:  
  var source2 = source.Select (s => s.ToNonNullableParentVersion ()); Edit: From your edit, it appears now that you do not need a different type to show the projected data, because 'augmented' property still remains empty if you do not want to replace existing archive And you do not like your current query, your best bet would still be to write the conversion method in source-type.   

Comments