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++
An array inside an array is called a nested array. I don't use these much in PowerShell but I have used them more in other languages. Consider using an array of arrays when your data fits in a grid like pattern. Here are two ways we can create a two-dimensional array.
TableName is the specified table holding array values in the database to use MySQL WHERE IN Array. After WHERE clause, specific column name is defined from which the data is retrieved using SELECT statement and from which the IN () function includes the range of values as parameters. How WHERE IN Array works in MySQL?
Adding to arrays 1 Array addition. We can use the addition operator with arrays to create a new array. We can add them together to get a new array. 2 Plus equals +=. Just remember that every time you use += that you're duplicating and creating a new array. ... 3 Pipeline assignment. You can assign the results of any pipeline into a variable. ...
The array is created as a sequential chunk of memory where each value is stored right next to the other. I'll touch on each of those details as we go.
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.
Where
narrows down the input sequence to a subset. That's not exactly what you want here, which is probably more like
var tempSpectrum = currentSpectra
.SingleOrDefault(c => c.filename == bgSpec.Filename);
SingleOrDefault
will return either the Spectrum
instance your are looking for or (assuming Spectrum
is a reference type) null
if no such spectrum exists. It will also throw an exception if there are multiple spectra that match the search parameter.
If this is not exactly what you want, look also into Single
and First
/FirstOrDefault
.
There are also non-LINQ alternatives to most of these: Array.Find
and several more static Array.FindX
methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With