How exactly to use array.where?
.net, arrays, c#
Solution
It looks like you're looking for a single value meeting that criterion. So maybe use `Single`:
var tempSpectrum = currentSpectra.Single(c => c.filename == bgSpec.filename);
Other options:
- `First`
- `FirstOrDefault`
- `SingleOrDefault`
- `Last`
- `LastOrDefault`
The `OrDefault` versions will return `null` if there's no matching element. The difference between `First`, `Single` and `Last` is in terms of the result for multiple matches: `Single` will throw an exception, whereas `First` or `Last` will take the first or last match respectively.
Which of these is most appropriate will depend on exactly what you want to do.
Problem
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. mySpectrum.filename; //String Now I have a stringvalue that I want to compare with each object in the array to find out, whether it has the same filename. As I got it it should work like this: ``` 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); ``` I propably got everything wrong and would be very grateful if somebody could point out what it is or how to do it right. Thanks in advance, BC++