Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello again dear programmers,

I'm struggling for a couple of days with a problem regarding my linq query.


When I'm debugging and I hover with my mouse over education,the value(which is a string) is empty.The worst part is that when I'm replacing it with a string from my database,it works perfectly.I did't post the binding part since it works when i replace"education" with a normal string,but if it has any relevance,please tell me so that I can post it.What would be the issue for the empty string?I searched for an answer an tried many approaches,but all of them failed.I'm using linq with sql.Thank you!

What I have tried:

This is my current code:
ViewModel:
private String education;
      public String Education
      {
          get { return education; }
          set
          {
              if (education != value)
              {
                  education = value;
                  NotifyOnPropertyChange("Education");
              }
          }
      }
This is the string that i want to pass the value to.

This is my method:
public List<Cours> CreateCrazy()
     {
         using (DatabaseStudentsEntities1 db = new DatabaseStudentsEntities1())
         {
             if(education==null)
             {
                 education = "";
             }
             try
             {

                 var query = (from data in db.Courses where data.education==education select new { CourseName = data.courseName }).ToList().Select(c => new Cours { courseName = c.CourseName }).ToList();
                 return query.ToList();

             }

             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message);
             }
         }
         return null;
     }
"education" is the null value.
Posted
Updated 17-Mar-18 17:33pm
Comments
Maciej Los 16-Mar-18 11:36am    
public List<Cours> CreateCrazy()
{
using (DatabaseStudentsEntities1 db = new DatabaseStudentsEntities1())
{
if(education==null)

Where education comes from in above piece of code?
#realJSOP 16-Mar-18 19:25pm    
In the database, are there any occurrences of a null education column value?
Daniel Andrei Popescu 19-Mar-18 6:22am    
Hello,no there are not null values in the education column.Instead,there are 4 values that i have binded to a combobox.

1 solution

As Maciej pointed out in his comment, your issue is most likely the following:
C#
private String education; //implied default of null
public String Education
{
    get { return education; } //simply a return
}

public List<Cours> CreateCrazy()
{
    using (DatabaseStudentsEntities1 db = new DatabaseStudentsEntities1())
    {
        //Education/education will always be null if it hasn't been set yet.
        if(education==null)
        {
            education = "";
        }
    }
}

My best guess is that it doesn't work because however you are normally testing it education isn't being set so it is null which gets defaulted to "" inside CreateCrazy. When you replace education with a hard-coded string it works because there's nothing technically wrong with the LINQ query. It's most likely a problem in your binding.

Additional Thoughts:
Properties: Always set the default on a property or field if needed instead of defaulting inside a method. This keeps variable state consistent. As the code is written now there are two default states depending on whether CreateCrazy has been called - null and "".

Functions: If possible it's good practice to try to maintain a single exit-point for functions. It makes the code easier to read, follow, and therefore maintain. This ties into another good practice in my opinion - eliminate as much null reliance as possible. If you call a function that queries and returns a results array, wouldn't you expect "no results" to just be an empty array? This also allows the consuming code to be simplified. Instead of having to null check you can simply iterate. If there are no results the loop will just exit immediately.

LINQ: Way too many ToList calls. ToList forces an evaluation of the LINQ expression so never ToList a query for results unless finished with it. Both calls could be removed from the query and just leave the ToList on the return value. Also the second part of the query isn't necessary. You're capturing courseName into an anonymous object and then transferring that data into a typed object in another select statement. Type it the first time.

Modified with suggestions:
C#
private String education = string.Empty;
public String Education
{
    get { return education; }
    set
    {
        if (education != value)
        {
            education = value;
            NotifyOnPropertyChange("Education");
        }
    }
}

public List<Course> CreateCrazy()
{
    List<Course> courseResult = new List<Course>();
    using (DatabaseStudentsEntities1 db = new DatabaseStudentsEntities1())
    {
        try
        {
            var query = from data in db.Courses
                        where data.education == Education
                        select new Course { 
                            CourseName = data.courseName 
                        };
            courseResult.AddRange(query);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    return courseResult;
}
 
Share this answer
 
v5
Comments
Daniel Andrei Popescu 19-Mar-18 5:16am    
Hello,thank you so much for your response.I have tried the version that you gave me but I still get n error at "CourseName" saying that my table "Cours" does not contain a definition for it.Since I take your advice into consideration,I have also tried instead of"select new Cours{CourseName = data.courseName}"-"select new {CourseName = data.courseName}" but in this case i have an error at the query saying that the type of the query is not compatible as IQueryable.Beside this,I understand your logic from the solution and I will try to adapt it as much as I can and hope it will work.

Thank you and best regards,
Jon McKee 19-Mar-18 16:52pm    
The first problem is probably because of casing. Your property is courseName on Cours. I always Pascal-case property names so I wrote it CourseName :P

Not sure on the second issue. LINQ to Entities can be touchy with select statements. It might be complaining if Cours doesn't have a public default parameterless constructor.
Daniel Andrei Popescu 19-Mar-18 6:38am    
I found the solution.It was all in the binding at the .xaml file because after a short research about the actual problem,i discovered that the method was good,but i had to have in the combobox:"Text={Binding Education}" and it works perfectly.
Jon McKee 19-Mar-18 16:38pm    
Glad to hear you figured it out! Binding can be a pain sometimes.
Daniel Andrei Popescu 20-Mar-18 6:32am    
Yes,i know...Thank you very much for your help!Have a beautiful day :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900