简体   繁体   English

使用Linq为另一个对象列表中的每个项目创建新的对象列表

[英]Create new list of objects for each of the item in another object list using Linq

I have an object with 我有一个对象

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

I have a List<test> . 我有一个List<test> For each of the item si want to create a new object with properties from obj1 and an item . si要为每个item创建一个具有obj1item属性的新对象 The items list can be null or empty. items列表可以为null或为空。 Is there a way to do in Linq ? Linq有办法吗?

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? 这是你想要的?

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. 假设Test.Obj1具有两个要包含在每个新项目中的属性,即“名称”和“ ID”,以及列表中的一个项目-尝试这样的查询。

Will generate a list of new (anonymous) objects with (for example) the first element of each Test.Obj1.List 将使用(例如)每个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 对于new {},您可以根据需要替换为命名类型T和构造函数,并将结果列表更新为

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() 另外,您可能对要(或全部)列表中的哪个项目有特定的条件,因此可以在构造函数中添加一个更有趣的查询,而不是.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://docs.microsoft.com/en-us/dotnet/csharp/linq/perform-a-subquery-on-a-grouping-operation 希望对您有所帮助-此集合中有一些很棒的链接: https : //docs.microsoft.com/zh-cn/dotnet/csharp/linq/perform-a-subquery-on-a-grouping-operation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM