1

As the title says. I have a page with a Job that have an ID. This ID I am storing in a Session and when I am trying to access the Company profile I just test if the Session Variable with Job ID from the tables Jobs and then I am trying to select the User ID and then with this User ID I search in UserManager and try to populate the page with some data.

Code For Company Profile:

    public async Task<ActionResult> Details(string id)
    {
        String JobValID = Convert.ToString(Session["DetailsURL"]);

        var UserJobID = context.Jobs.Where(x => x.ID.ToString() == JobValID).OrderByDescending(x => x.UserID).Select(x => x).ToString();
        UserJobID = id; //This line is to see what value I got from LINQ and is still the Session value.

        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        var user = await UserManager.FindByIdAsync(id);
        return View(user);

      }

View For this Controller is default scaffolded View from controller.

And like This I get Session["DetailsURL"]

var currentID = Url.RequestContext.RouteData.Values["id"];          
Session["DetailsURL"] = currentID;

I guess this is not the standard way to get a User Profile page. But I am using an ActionLink from Job Page to get into the Company Profile.

Here ActionLink to get from JobPage to Company Profile:

@Html.ActionLink("About this company", "Details", "UserAdmin", new { id = Session["UserJobID"] }, null)

My problem is in LINQ statement, when I am trying to select UserID, it doesn't get the right value, and when I am searching in UserManager the value is still the session Value.

Found the solution here So how I did it:

    var UserJobId = (from c in context.Jobs
                     where c.ID == JobValID
                     select new { c.UserID }).Single();

    var myVal = UserJobId.UserID;
    id = myVal;
Community
  • 1
  • 1
Eduard
  • 732
  • 1
  • 7
  • 20
  • Your linq statement is confusing me. Why are you calling `ToString()` on a collection ? also what is the use of `Select(x=>x)` ? – Shyju Oct 06 '16 at 01:03
  • I called`ToString` just to match with string id type, and my UserManager asks for a `string`. And at first it was without `OrderByDescending(x => x.UserID).Select(x => x)` but the value is the same. – Eduard Oct 06 '16 at 01:06
  • 1
    I feel like you expect `OrderByDescending(x => x.UserID).Select(x => x)` to do something it is not capable of doing. Are you trying to get a `Single` user id? – czifro Oct 06 '16 at 03:03
  • Yea I am trying get a `single value` so I can search it in `UserManager` and get the profile of a User. `UserID value` is the value of current user that posts the Job. I just want to `select` that value where the row `x =>x.ID == Session["Value"]`. Sessions values are not messed up only `LINQ` is not selecting what I am asking and pass forward the `Session Value`. Maybe is a problem the `UserID` value is a `GUID`? – Eduard Oct 06 '16 at 12:50

1 Answers1

1

It has been awhile since I have used C#, but your LINQ statement seems to be getting a list of Job objects and doing ToString on the list. If Job has reference to the UserId associated with it, then the following LINQ statement should work:

var userId = context.Jobs
                  .SingleOrDefault(x => x.ID.ToString() == jobId)
                  .?Select(j => j.<accessorToUserId>) // Not sure what your schema is
                  .?ToString()

Notice the ?. This is just in case SingleOrDefault returns null then the following method calls won't happen. If, for some reason, more than one Job uses that jobId, which is really bad practice, replace SingleOrDefault with FirstOrDefault.

Also, Select is supposed to be used to map from type T to type U. Doing Select(x => x) is pointless since you are simply doing T -> T.

czifro
  • 784
  • 7
  • 24
  • Thanks for inspiration with Single thing, but I did it with another approach. [http://stackoverflow.com/questions/6015081/c-sharp-linq-how-to-retrieve-a-single-result](Found here). Your suggestion with `Single` made me search in the right places. I will make edit how I did It. – Eduard Oct 07 '16 at 03:36
  • I think they are the same, but I'm glad I could help – czifro Oct 07 '16 at 04:09