English 中文(简体)
具体如何使用阵列。 在哪里?
原标题:How exactly to use array.where?

I m not sure if I got the right idea of how array.where() works. I ve got an array filled with Spectrum.cs objects. each spectrum contains a filename-property.

我的特征. filename; // string

现在我有一个字符串值, 我想与数组中每个对象进行比较, 以找出它是否有相同的文件名 。 正如我所理解的, 它应该这样工作 :

Spectrum bgSpec = new Spectrum(); //Has a filename
Spectrum[] currentSpectra;  //Array filled with Spectra

//Somehow this doesn t seem to work. Propably due to the returnvalue for where() which seems //to be IEnumerable.
Spectrum tempSpectrum = currentSpectra.Where<Spectrum>(c => c.filename == bgSpec);

我可能把一切都弄错了,如果有人能指出它是什么或如何正确行事,我会非常感激。

Thanks in advance, BC++

最佳回答

看起来你正在寻找一个符合此标准的 值。 所以也许使用 < a href="http://msdn.microsoft.com/ en- us/library/ system. linq. enumberable. single.aspx" rel="noreferr"\\\ code>Single :

var tempSpectrum = currentSpectra.Single(c => c.filename == bgSpec.filename);

其他选项 :

OrDefault 版本将返回 null , 如果没有匹配元素的话。 first , single single 之间的差别是多个匹配的结果: single 将丢出一个例外,而 first Last 将分别选择第一个或最后一次匹配。

其中哪一种最合适取决于你到底想做什么。

问题回答

< where 将输入序列缩缩到子集。 这不是您想要的, 这可能更像

var tempSpectrum = currentSpectra
                   .SingleOrDefault(c => c.filename == bgSpec.Filename);

SingleOrDefault 将返回您正在寻找的 Spectrum 实例,或者(假设 Spectrum 是一个参考类型) >null ,如果不存在这种频谱,则返回 SleOrDefault 。 如果有多个与搜索参数匹配的光谱, 也会丢出一个例外 。

如果这不是你想要的,请查看 Single First //FirstOrDefault

除了大多数这些方法外,还有非LINQ的替代方法:Array.Find 和几种更静态的Array.FindX 方法。

如约翰所说, single 在此是合适的。 如果您有多个频谱对象, 可能符合条件, 那么您应该使用频谱对象列表代替。 您还应该比较 filename 属性

IEnumerable<Spectrum> tempSpectrums = currentSpectra.Where(c => c.filename == bgSpec.filename);
Spectrum tempSpectrum = currentSpectra.SingleOrDefault(c => c.filename == bgSpec.filename);

请注意,您不需要为诸如 < code> where , single , , SingleOrDefault 等方法指定通用类型 -- -- 汇编者将从收藏类型中推断类型。

使用 single SingleOr Default 在您的 中使用 > singleOr Default 而不是 where

var tempSpectrum = currentSpectra.SingleOrDefault(c => c.filename == bgSpec.filename); 




相关问题
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. ...