6 Replies - 15676 Views - Last Post: 29 March 2010 - 08:30 AM Rate Topic: -----

#1 eamon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 25-March 10

Get value from Linq query

Posted 29 March 2010 - 06:39 AM

Hello

I want to get a value from a Linq query, e.g. I have a database called Team, I retrieve a certain team using the teamId, and then I want to assign the team name to a string...is this possible?

I have the query working to retrieve the team, it is as follows:

private Team getTeam(int teamId)
        {
            return (from t in db.Team where t.TeamId == teamId select t).Single();
        }


Is it possible to use the above to assign any of the values retrieved to string? e.g. something like...
string teamName = getTeam(1).teamName.toString();

I know this isnt right but is something like this possible?

Any help will be much appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Get value from Linq query

#2 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Get value from Linq query

Posted 29 March 2010 - 06:41 AM

I believe it is, but your Team class then needs to have public TeamName property.
Was This Post Helpful? 0
  • +
  • -

#3 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Get value from Linq query

Posted 29 March 2010 - 06:53 AM

private Team GetTeam(int id)
{
    return (from t in db.Team where t.TeamId == id select t).SingleOrDefault();
}

private void AssignTeamName(Team team)
{
    string teamName;

    if (team != null)
        teamName = team.TeamName;
}



You need to know that there is always a possibility that your database does not contain a record for a team with id you requested. Because of that I suggest that you call .SingleOrDefault() on the end of your linq query, where default mean - return a null value for the team if no record with this id is found. Then you must check if your team instance is null and if not, you can get it's TeamName property.
Was This Post Helpful? 2
  • +
  • -

#4 eamon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 25-March 10

Re: Get value from Linq query

Posted 29 March 2010 - 07:22 AM

Thanks FlashM

Your code put me on the right track. It was working until I had a member that was part of more than one team, that made the SingleOrDefault throw an exception. So I changed it so that it returns as a toList, then I loop through the list getting the teamName. The code is as follows:
private string getTeamName(int memberId)
        {
            string teamName = "";

            List<TeamMember> teamMember = (from tm in db.TeamMembers
                                    where tm.MemberId == memberId
                                    select tm).ToList<TeamMember>();

            if (teamMember == null)
            {
                teamName = "";
                return teamName;
            }
            else
            {
                foreach (TeamMember tm in teamMember)
                {
                    Team team = (from t in db.Teams
                                 where t.TeamId == tm.TeamId
                                 select t).SingleOrDefault();

                    teamName += team.Name.ToString();
                }
                return teamName;
            }
        }


Thanks again, problem solved!
Was This Post Helpful? 0
  • +
  • -

#5 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Get value from Linq query

Posted 29 March 2010 - 07:56 AM

Well, I think this solution is a bit weird... What is your database schema? I'm not here to judge, but I believe if you want to retrieve a member with given id, you should always get only one member or no records returned. In your case you have more than one member with same member id, which is not really logical. This would make sense if you had two members with the same id, but one member would have for example deleted flag set on true or something similar.
Was This Post Helpful? 0
  • +
  • -

#6 eamon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 25-March 10

Re: Get value from Linq query

Posted 29 March 2010 - 08:06 AM

I've got three tables - Member, Team, & TeamMember.

Member has all member details, e.g. memberId, name, address, etc.

Team has all team details, e.g. teamId, name, address, etc.

And TeamMember is a table to create a link between the above two, so it has a composite key using teamId and memberId.

My logic behind this is that a member can be part of more than one team, e.g. a member can be in a basketball team, but can also be in a football team, thats why a member can appear a number of times in the TeamMember table.
Was This Post Helpful? 0
  • +
  • -

#7 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Get value from Linq query

Posted 29 March 2010 - 08:30 AM

Oh, I see... Then it's OK... So one member can be member of different Teams...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1