C# Linq 从列表中返回一个新对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17831244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:36:24  来源:igfitidea点击:

Linq to return a new object with a selection from the list

c#linq

提问by flip

I have the following object structure:

我有以下对象结构:

public class A
{
    public string ID { get; set; }
    public IList<B> Values { get; set; }
}

public class B
{
    public string Code { get; set; }
    public string DisplayName { get; set; }
}

public List<A> IDs;

I would like to use Linq to query B and return a single instance of A with the single element of B in values. Is that possible? I currently do this with a foreach but I am thinking Linq would be neater.

我想使用 Linq 查询 B 并返回 A 的单个实例,其中 B 的单个元素在值中。那可能吗?我目前使用 foreach 执行此操作,但我认为 Linq 会更简洁。

foreach (A a in IDs)
{
    foreach (B b in a.Values)
    {
        if (b.Code == code)
        {
            return (new A()
            {
                ID = a.ID,
                Values = new List<B>()
                {
                    new B()
                    {
                        Code = b.Code,
                        DisplayName = b.DisplayName
                     }
                 }
            });
        }
    }
}

采纳答案by Cédric Bignon

In LINQ with the query-syntax:

在带有查询语法的 LINQ 中:

return (from a in IDs
        from b in a.Values
        where b.Code == code
        select (new A
        {
            ID = a.ID, Values = new List<B>
            {
                new B
                {
                    Code = b.Code, 
                    DisplayName = b.DisplayName
                }
            }
        })).FirstOrDefault();

回答by Hogan

Run the following in LinqPad (LinqPad.com)

在 LinqPad (LinqPad.com) 中运行以下命令

void Main()
{
    List<A> IDs= new List<A>() {
        new A() { ID = "1", Values = new List<B>() {
                             new B { Code = "1", DisplayName = "1"}, 
                             new B { Code = "2", DisplayName = "2"},
                             new B { Code = "3", DisplayName = "3"} } },
        new A() { ID = "4", Values = new List<B>() { 
                             new B { Code = "4", DisplayName = "4"}, 
                             new B { Code = "5", DisplayName = "5"},
                             new B { Code = "6", DisplayName = "6"} } },
        new A() { ID = "7", Values = new List<B>() { 
                             new B { Code = "7", DisplayName = "7"}, 
                             new B { Code = "8", DisplayName = "8"},
                             new B { Code = "9", DisplayName = "9"} } }
    };

    A result = IDs.Where(a => a.Values.Any(b=> b.Code == "4")).FirstOrDefault();
    result.Dump();
    result = IDs.FirstOrDefault(a => a.Values.Any(b=> b.Code == "8"));
    result.Dump();

}

// Define other methods and classes here
public class A
{
    public string ID { get; set; }
    public IList<B> Values { get; set; }
}

public class B
{
    public string Code { get; set; }
    public string DisplayName { get; set; }
}

You get this:

你得到这个:

result

结果



Prior edits follow:

之前的编辑如下:

With the edit to the question:

随着对问题的编辑:

A result = IDs.Where(a => a.Values.Any(b=> b.Code == code)).FirstOrDefault();


Original answer below

原答案如下

The following will return the first A element where ID = id

下面将返回第一个 A 元素,其中 ID = id

A result = IDs.Where(a => a.ID == id).FirstOrDefault();

This makes it a list

这使它成为一个列表

List<A> result = IDs.Where(a => a.ID == id).FirstOrDefault().ToList();

回答by Abhishek Jain

Try this:

尝试这个:

IDs.Where(a=>a.ID = id)
   .Select(a => new A() 
   {
       ID = a.ID,
       Values = new List<B>()
       {
           new B() 
           { 
               Code = a.Values.First().Code, 
               DisplayName = a.Values.First().DisplayName 
           }
       }
    });