0

I have an object with

public class test
 { 
     public object obj1;
     public List<string> items;
 }

I have a List<test>. For each of the items i want to create a new object with properties from obj1 and an item. The items list can be null or empty. Is there a way to do in Linq?

5
  • 2
    The answer is yes, probably, maybe, no. You'll need to be more specific, describe why you need object type for obj1 whats in it, and some test data and sample output Commented Aug 8, 2019 at 6:04
  • probely what you are looking for is SelectMany Commented Aug 8, 2019 at 6:43
  • 1
    Could you provide an example? Commented Aug 8, 2019 at 7:07
  • 1
    Can you describe, in more detail, the object you want to construct? Right now I'm having trouble imagining what you want. Thanks. Commented Aug 8, 2019 at 7:28
  • Which properties from obj1? Commented Aug 8, 2019 at 17:35

2 Answers 2

2
List<test> tests = new List<test>();
var ob1=new test{ obj1 = "obj1" };
var ob2=new test{ obj1 = "obj2" };
var ob3=new test{ obj1 = "obj3" };
var ob4=new test{ obj1 = null };
tests.Add(ob1);
tests.Add(ob2);
tests.Add(ob3);
tests.Add(ob4);

var result = tests.Select(e => new NewType
{
     name = e.obj1 != null ? e.obj1.ToString() : null
});

foreach (var item in result)
{
    Console.WriteLine(item.name);
}

Is this what you are looking for?

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.