Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7970427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:23:51+00:00 2026-06-04T07:23:51+00:00

My code: public string LocName(string locID) { var name = (from a in idc.Locations

  • 0

My code:

public string LocName(string locID)
    {
        var name = (from a in idc.Locations
                    where a.ID.ToString() == locID
                    select a.Name).Single();
        return name;   
    }

I need it to return the name of the Location that matches the ID as a string for a TextBlock. Instead it returns no elements, I have checked that the ID is correct so it should return at least one element.

I have also tried:

where a.ID == new Guid(locID)

as well as

.First()
.FirstOrDefault()

have tried returning the result as an element and databinding to that but still – no elements.

What is the error???

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:23:52+00:00Added an answer on June 4, 2026 at 7:23 am

    Must be a casing issue if the data is there, you should always use ToUpper() as that is optimised for string equality checks.

    Also you should always use FirstOrDefault as Single will throw an exception if there are more than one matches and First will throw an exception if there are no matches. Try:

    EDIT: I’ve also added Trim on the inputted value to sanitise the spaces. I’ve also added a null check on the locID parameter as that would blow up if it was passed is as null. Lastly, I added a ?? (coalesce) on the return statement just incase it is returning null and you’re performing other things on that string (such as Trim or ToLowerCase) as that would result in an exception.

    public string LocName(string locID)
    {
        if (locID == null) return string.Empty;
    
        var name = (from a in idc.Locations
                    where a.ID.ToString().Trim().ToUpper() == locID.Trim().ToUpper()
                    select a.Name).FirstOrDefault();
    
        return name ?? string.Empty;   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider following code public class City { public string Name { get { return
I have this simple code public String toString() { **Iterator it = list.iterator();** String
Consider the following lines of code: public interface IProduct { string Name { get;
The code: public String getTemperatureMessage(double temp) { if(temp < 32) return Freezing; else if(temp
Code: public String get() { try { //doSomething return Hello; } finally { System.out.print(Finally);
here is the basic code: public static string DeserializeNames() { jsonData = {\name\:[{\last\:\Smith\},{\last\:\Doe\}]}; JavaScriptSerializer
I have this piece of code: public string[] GetUsergroupRoles(string username) { var qry =
I have the following code : public class MyClass { private readonly string name;
I got the following piece of code: public class Collect { public string name{
My Code: public class Contact { public string id{ get; set; } public string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.