English 中文(简体)
利用林研中心从具有零元素的财产中收集物
原标题:Using LINQ to populate a collection from a property that has n elements
  • 时间:2011-11-22 19:05:00
  •  标签:
  • c#
  • linq

我有以下几类:

public class A
{
   public List<object> MyItems { get; set; }
   public Color Color { get; set; }
   public object MyItem { get set; }
}

每一例A均可有n。 MyItems.

鉴于一份名单,我需要以色谱加以过滤,并制定新的清单,其中为MyItems收集的每个要素创建了一个新的A案例。

List<A> Aitems = originalItems.Where(b => b.color == color)
                              .Select(b=>
{
    A aItem = b;
    // Problem below. Is there a way to create more
    // aItems for every object in MyItems collection?
    b.MyItem = b.MyItems[0];
    return aItem;
}).ToList();

是否有办法利用准则收集MyItems的每一物体制造更多的系统,或者我是否应当使用标准表格?

最佳回答

你们是否寻求这样的东西?

var query = from b in originalItems
            where b.Color == color
            from item in b.MyItems
            select new A
            {
                MyItems = b.MyItems,
                Color = b.Color,
                MyItem = item,
            };

var result = query.ToList();
问题回答

假设你想为<代码>中的每一项目创造新的A。 具体彩色物体的MyItems,你可以这样做:

from a in originalItems
where a.color == color
from myItem in a.MyItems
select new A { /* not sure what you want here */ }

这将使你们能够从原计划获得一份清单,其中你希望:

var items = (from a in OriginalItems
              where a.Color == color
              from item in a.MyItems
              select item).ToList();




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签