question

Karl-6018 avatar image
0 Votes"
Karl-6018 asked Viorel-1 edited

Set a List item in a class to a new list item and add to it in a LINQ query

So lets say I have a class object called SomeObject, and one called NamesObject

     public class NamesObject 
     {
       public string FirstName {get; set;}
       public string LastName {get; set;}
     }
    
     public class SomeObject
     {
       public List<NamesObject> Names {get; set;}
     }

Then I do a LINQ query that ends with a select of a new SomeObject, I want to do something like this, but I can't work it out, or even if you can do it?

 var N = (from x in sometable
 select new SomeObject {
  Names = new List<NamesObject>().Add(new NamesObject { x.FirstName, x.LastName })
 });





dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
2 Votes"
Viorel-1 answered Viorel-1 edited

Try something like this:

var N = new SomeObject { Names = (from x in sometable select new NamesObject { FirstName = x.FirstName, LastName = x.LastName }).ToList() };


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.