0

I am trying to query my database to get a specific data from my database. however when I convert the query to string it doesn't return the select value, instead it returns the whole SQL Query in a string. I am stumped on why this is happening

public ActionResult StudiedModules()
    {
        Model1 studiedModules = new Model1();
        List<StudiedModulesModel> listModules = new List<StudiedModulesModel>();
            using (EntityOne context = new EnitityOne())
            {
                foreach(var module in context.StudiedModules){
                    studiedModules.School = context.ModuleDatas.Where(p=>p.ModuleCode == module.ModuleCode).Select(u=>u.School).ToString();
                    studiedModules.Subject = context.ModuleDatas.Where(p=>p.ModuleCode == module.ModuleCode).Select(u=>u.Subject).ToString();
                }
            }

            var data = listModules;

        return View(data);
    }
0

2 Answers 2

1

Calling ToString() on an Entity Framework Linq query like that will in fact return the SQL Query. So as it's written, your code is doing exactly what you wrote it to do.

If you want to select the first result from the IQueryable<T>, then you need to call First() before your ToString(). So, try changing

studiedModules.School = context.ModuleDatas.Where(p=>p.ModuleCode == module.ModuleCode).Select(u=>u.School).ToString();

studiedModules.Subject = context.ModuleDatas.Where(p=>p.ModuleCode == module.ModuleCode).Select(u=>u.Subject).ToString()

to

studiedModules.School = context.ModuleDatas.Where(p=>p.ModuleCode == module.ModuleCode).Select(u=>u.School).First().ToString();

studiedModules.Subject = context.ModuleDatas.Where(p=>p.ModuleCode == module.ModuleCode).Select(u=>u.Subject).First().ToString()

There are a whole lot more methods available depending on what you're trying to accomplish. If you want to get a list, use ToList(), or as Uroš Goljat pointed out, you can get a comma-separated list of values using the Aggregate( (a, b)=> a + ", " + b) method.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your help, I have used First() - Which gives an error "Sequence contains no elements". So I used FirstOrDefault() which does not seems to get the correct values that I need, it seems to always get the first item within the database for all rows.
The "Sequence contains no elements" is the exception that First() throws when there are no results to your query. The difference between First() and FirstOrDefault() is that if there are no results from the query, First() will throw that exception, and FirstOrDefault() will return null.
This may sound stupid, but I have found when I add to my list within the foreach loop it overwrites all previous data with current one while adding itself. Do you know why this may occur?
You're not adding to your listModules instance of List<StudiedModulesModel>. You're just repeatedly changing your studiedModules instance of Model1.
Thanks Tim, you were right, I needed to create a new studiedModules for each loop. Thanks
0

How about using Aggregate( (a, b)=> a + ", " + b) instead of ToString().

Regards, Uros

Comments

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.