0

I have a schedule object that is returned from the database. It contains information from a few tables. One of the tables is called ScheduleData and has four columns. It has this format:

Id  |  ScheduleId |  Name  |  Value

I need the value of the fourth column where the Name is Mine and the ScheduleId is 5

I have tried this, but it doesn't work:

string val = from s in schedule.ScheduleData where s.Name.Equals("Mine") && s.ScheduleId == 5 select s.Value;

1 Answer 1

2

Use First method or FirstOrDefault method.The query returns an IEnumerable<T>, you can't assign it to string.

string val = (from s in schedule.ScheduleData 
             where s.Name == "Mine" && s.ScheduleId == 5 
             select s.Value).First();
Sign up to request clarification or add additional context in comments.

2 Comments

I tried it, but it gives me "InvalidOperationException". It says "Sequence contains no elements".
then use FirstOrDefault and check for null. the error indicates, you have no item in your database that matches with your criteria

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.