1

im having an issue converting a query to a string...i did my research and found the majority of the answers were to attach first or single to the end of the query, which can be clearly seen.I thought this might of been a cache problem so cleaned the solution. in which i get a error below

Error 1 Cannot implicitly convert type 'JobTracker.Models.Location' to 'string'

Code in question:

public static string ClientIPName()
{
    string ClientIP = null;
    ClientIP = HttpContext.Current.Request.UserHostAddress;
    return ClientIP;
}

public static string LocationIPAssign()
{
    JobData db = new JobData();

    var workstationLocation = ClientIPName();
    var result = db.Locations
        .Where(l => l.AssignedIP == workstationLocation)
        .FirstOrDefault();

    return result;
}

Initial error:

Cannot implicitly convert type System.Linq.IQueryable to string

Any ideas what could be causing this error?

Alex
  • 13,024
  • 33
  • 62
Matchbox2093
  • 986
  • 2
  • 12
  • 40
  • `result` is not the type your function expects to return? – ps2goat Apr 24 '15 at 22:33
  • 1
    You are getting error messages related to types that don't match. Yet your question does not include any information on what they might be. Please [edit] your question, and add the relevant parts of their declaration, including the expected return type for the function that contains `return result;` – Alex Apr 24 '15 at 22:34
  • What line is the exception actually occurring? On that line hover your mouse over each type to figure out where the conversion is trying to take place. – Yuriy Faktorovich Apr 24 '15 at 22:36
  • i get an error at "return result" @YuriyFaktorovich – Matchbox2093 Apr 24 '15 at 22:39
  • 1
    @goldeneye sounds like the return type is string, while result is Location then. Gotta pick one. – Yuriy Faktorovich Apr 24 '15 at 22:40
  • @YuriyFaktorovich i used the answer below involving a select attribute, done exactly i wanted... problem resolved :) – Matchbox2093 Apr 24 '15 at 22:42

1 Answers1

4

Your FirstOrDefault call is resulting in a Location object and you obviously want a string. My guess would be that one of the properties on the Location is what you're trying to return, so select that value off.

var result = db.Locations
    .Where(l => l.AssignedIP == workstationLocation)
    .Select(l => l.<whatever string property you want>)
    .FirstOrDefault();
Craig W.
  • 17,838
  • 6
  • 49
  • 82