Say Test.Obj1 has two properties you want to include in each new item, 'Name' & 'ID', along with an item from the list - try a query like this.
Will generate a list of new (anonymous) objects with (for example) the first element of each Test.Obj1.List
IEnumerable<dynamic> results = tests
.Select(o => new { Name = o.obj1.Name,
ID = o.obj1.Id,
Item = o.items.First() ?? null} )
.ToList();
Console.WriteLine(results.ElementAt(0));
Console.WriteLine(results.ElementAt(1));
Console.WriteLine(results.ElementAt(2));
// { Name = Foo, ID = 1, Item = a }
// { Name = Bar, ID = 2, Item = d }
// { Name = Goo, ID = 3, Item = g }
// Example types below...
public class Obj1
{
public string Name {get; set;}
public int Id {get; set;}
public Obj1(string name, int id)
{
Name = name;
Id = id;
}
}
// Create some instances
Obj1 objA = new MyObject("Foo", 1);
Obj1 objB = new MyObject("Bar", 2);
Obj1 objC = new MyObject("Goo", 3);
// Your test class that contains the custom object, and a list
public class Test
{
public Obj1 obj1;
public List<string> items;
public Test(Obj1 myobject, List<string> myItems)
{
obj1 = myobject;
items = myItems;
}
}
// Make a list of test objects
List<Test> tests = new List<Test>{
new Test(objA, new List<string>{"a", "b", "c"}),
new Test(objB, new List<string>{"d", "e", "f"}),
new Test(objC, new List<string>{"g", "h", "i"})};
For new{}, you can replace with a named type T and constructor as needed and update the results list to be
IEnumerable<T>
Also, you may have a specific criteria for which item in the list you want (or all) so you could add a more interesting query in the constructor rather than .First()
Or to just grab the whole test item adjust the constructor as
new { Obj = o.obj1, Items = o.items}
Hope this helps - there are some great links in this collection: https://learn.microsoft.com/en-us/dotnet/csharp/linq/perform-a-subquery-on-a-grouping-operation
objecttype forobj1whats in it, and some test data and sample outputSelectManyobj1?