7 Replies - 692 Views - Last Post: 02 September 2016 - 05:28 PM Rate Topic: -----

#1 ScottinTexas   User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 310
  • Joined: 13-March 12

Having trouble with this LINQ syntax

Posted 30 August 2016 - 01:31 PM

Hi all,

I am trying to extract a value from a collection (observableCollection) of objects. SampleID is one of the object members and I want the SampleID from the sample object where the sample object has has a given testID.
            var mw=new MainWindow();
            var testID="27189888";
            string sampID = mw.Samples.Where(smp => smp.TestID == testID).First().SampleID;
            Assert.AreEqual("876818", sampID);



I am getting an exception "Sequence contains no elements." But I don't understand the statement. That line is wrong but I am not sure how the syntax should be. There are no compiler errors.

Thanks,

Scott

This post has been edited by ScottinTexas: 30 August 2016 - 01:42 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Having trouble with this LINQ syntax

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,231
  • Joined: 12-December 12

Re: Having trouble with this LINQ syntax

Posted 30 August 2016 - 01:57 PM

You are creating a new MainWindow, is it automatically populated with Samples?

Break the code down. Examine the Samples first, do they exist and contain the values/items you expect them to contain?
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,231
  • Joined: 12-December 12

Re: Having trouble with this LINQ syntax

Posted 30 August 2016 - 02:30 PM

It is also preferable to use FirstOrDefault() unless you have some guarantee that the sequence will always have at least one element.
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Having trouble with this LINQ syntax

Posted 30 August 2016 - 04:34 PM

The Where extension method is returning no results. The First extension method requires that the IEnumerable it's operating on have at least one element, or it throws that particular exception. Same for Single, except that requires there to be one and only one element.

There are two other extension methods that are similar: FirstOrDefault and SingleOrDefault. These have the same requirements, with the exception that if they operate on an empty collection, they return default(T). In the case of classes, that's always null.

So, this is how you would write that:

var sample = mw.Samples.FirstOrDefault(s => s.TestID == testID);


Now, sample is either the Sample you're looking for, or it's null. If it's null, you didn't find a match. If not, you did.
Was This Post Helpful? 2
  • +
  • -

#5 JapanDave   User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 413
  • Joined: 01-February 16

Re: Having trouble with this LINQ syntax

Posted 31 August 2016 - 02:05 AM

I could not add anything to Curtis's post, but I was thinking something like this maybe?
string sampID = mw.Samples.Where(smp => smp.TestID == testID).Select(smp => smp.SampleID).FirstOrDefault();

This post has been edited by JapanDave: 31 August 2016 - 02:08 AM

Was This Post Helpful? 0
  • +
  • -

#6 hamid_hr   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 31-August 16

Re: Having trouble with this LINQ syntax

Posted 31 August 2016 - 05:38 AM

	var mw=new MainWindow();

	var testID="27189888";

	var q = mw.Samples.Where(smp => smp.TestID == testID)
	if(q.any()){
		Assert.AreEqual("876818", q.First().SampleID);
		...
	}


excuse me
	var mw=new MainWindow();

	var testID="27189888";

	var q = mw.Samples.Where(smp => smp.TestID == testID).Select(smp=>smp);
	if(q.any()){
		Assert.AreEqual("876818", q.First().SampleID);
		...
	}

Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Having trouble with this LINQ syntax

Posted 31 August 2016 - 07:17 AM

If you're doing a First/FirstOrDefault/Single/SingleOrDefault, you don't need a Where, because the single-element extension methods take a lambda function that filters, just like the Where method.

If you're using extension methods, you don't need Select(s => s);, because that does nothing. Select is a projection method, you're not projecting anything here, just saying "Select myself".

If you're using C# 6 syntax you can even make it shorter:

var sampleId = mw.Samples.FirstOrDefault(s => s.TestID == testID)?.SampleID;
if(!sampleId.HasValue) {
  //not found case
} else {
  //do something with sampleId.Value
}


Was This Post Helpful? 4
  • +
  • -

#8 JapanDave   User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 413
  • Joined: 01-February 16

Re: Having trouble with this LINQ syntax

Posted 02 September 2016 - 05:28 PM

@Curtis, of coarse, that makes sense. I will remember that for future reference. Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1