I am a bit new to C#. I am trying to retrieve a C# query and have some problems how to get my parameters i.e. id, location, name....

The result of query is in jobs and I want to retrieve jobs object to get all my data.

This is the code I have tried:

public .. searchRelevantJob(string skills, string location, string category)
{
    Database1Entities o = new Database1Entities();
    JobPost j = new JobPost();

    using (Database1Entities context = new Database1Entities())
    {
        // Query for all blogs with names starting with B
        var jobs = from b in context.JobPosts
                   where b.Locationn.Equals(location) && 
                         b.RequiredSkills.Equals(skills) && 
                         b.Department.Equals(category)
                   select b;               
        return jobs;
    }
}

1 Answers

1
Gilad Green On Best Solutions

A linq query returns a collection of the type specified in the select clause. As such you want your method to return IEnumerable<JobPost>:

public IEnumerable<JobPost> searchRelevantJob(string skills, string location, string category) { ... }

(You could also see that if you debug and check what is the type of jobs - var is not a magic word, it is just there to help with long type names, don't let it confuse you)

In addition, I personally would go with method syntax here:

return context.JobPosts.Where(b => b.Locationn.Equals(location) && 
                     b.RequiredSkills.Equals(skills) && 
                     b.Department.Equals(category));

If you are expecting only one item then you can use FirstOrDefault instead of Where. See also: Entity Framework 4 Single() vs First() vs FirstOrDefault()