Linq从列表中返回一个带有选择的新对象

时间:2013-07-24 10:22:07

标签: c# linq

我有以下对象结构:

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;

我想使用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
                     }
                 }
            });
        }
    }
}

3 个答案:

答案 0 :(得分:6)

试试这个:

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 
           }
       }
    });

答案 1 :(得分:3)

在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();

答案 2 :(得分:2)

在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; }
}

你明白了:

result


之前的修改如下:

编辑问题:

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

下面的原始答案

以下内容将返回ID = id

的第一个A元素
A result = IDs.Where(a => a.ID == id).FirstOrDefault();

这使它成为一个列表

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