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?

6
  • result is not the type your function expects to return? Commented Apr 24, 2015 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; Commented Apr 24, 2015 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. Commented Apr 24, 2015 at 22:36
  • i get an error at "return result" @YuriyFaktorovich Commented Apr 24, 2015 at 22:39
  • 1
    @goldeneye sounds like the return type is string, while result is Location then. Gotta pick one. Commented Apr 24, 2015 at 22:40

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

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.