• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 707
  • Last Modified:

LINQ - Reference Collection object inside of Collection for Query

Does anybody know why I wouldn't have access to the eventSportsInfo object inside the LINQ query below?

I have two collections in code area below, the EventSportsInfo collection is referenced inside the TeamsSportsInfo collection.

Then in LINQ query we create a new instance of TeamsSportsInfo but when inside the query, I only have access to eventSportsInfo and none of its properties.  The "key" property in example is not available, intellisense only displays "Equals" and "ReferenceEquals".  Don't pay attention to what I'm assigning to it right now, just trying to get access to those properties.

This works just fine outside of the LINQ query, I have full access to both classes of properties when creating a new instance of TeamsSportsInfo.

I have tried many other examples and they all don't allow access.  

Any ideas?
public class EventSportsInfo
        public EventSportsInfo() { }

        public String Id { get; set; }
        public String Key { get; set; }
        public String DateCoverageType { get; set; }
        public String DateCoverageValue { get; set; }
        public String Status { get; set; }
        public Nullable<DateTime> StartDate { get; set; }
        public String  Week { get; set; }
        public String SeasonType { get; set; }

public class TeamSportsInfo
        public TeamSportsInfo() { }

        public Int32  Id { get; set; }         
        public String Key { get; set; }                                                         
        public Int32  PublisherId { get; set; } 
        public Int32  HomeSiteId { get; set; } 
        public String Score { get; set; }
        public String Alignment { get; set; }
        public String EventOutcome { get; set; }
        public Int32  DisplayNameId { get; set; } 
        public String EntityType { get; set; }                                                         
        public String FullName { get; set; }                                                            
        public String FirstName { get; set; }             
        public String LastName { get; set; }        
        public String Alias { get; set; }    
        public String Abbreviation { get; set; }
	public EventSportsInfo eventSportsInfo { get; set; }


                List<TeamsSportsInfo> teamsSportsInfo =
                    (from team in Root.Descendants("team")
                     let teamMetaData = team.Elements("team-metadata")
                     let teamName = teamMetaData.Elements("name")
                     let firstName = teamName.GetFirstElementsAttributeValueAsString("first")
                     let lastName = teamName.GetFirstElementsAttributeValueAsString("last")
                     select new TeamsSportsInfo
                        eventSportsInfo.Key = "",
                        Key = teamMetaData.GetFirstElementsAttributeValueAsString("team-key"),
                        Alignment = teamMetaData.GetFirstElementsAttributeValueAsString("alignment"),                        
                        FirstName = firstName,
                        LastName = lastName,
                        FullName = firstName + " " + lastName,

Open in new window

  • 3
  • 3
1 Solution
Fernando SotoRetiredCommented:
Hi kruegerste;

You have a type O in the name TeamSportsInfo, in these line:

List<TeamsSportsInfo> teamsSportsInfo
select new TeamsSportsInfo

Should be:

List<TeamSportsInfo> teamsSportsInfo
select new TeamSportsInfo

There is an extra s after the m in Team.

Also eventSportsInfo is a getter/setter and you need to pass it a EventSportsInfo object and not to use it as an object of EventSportsInfo. So you will need to change the query to this to work here as well.

let eventInfo = new EventSportsInfo() { Key = "" }
select new TeamSportsInfo
    eventSportsInfo = eventInfo,

kruegersteAuthor Commented:
thanks Fernando, that is sweet, that helps so much.  

Sorry about the typo, it came from me trying to put together a dummy example.  Our actual collection and queries are so huge that I try to provide a more simple example to keeps things from getting confusing.

Thanks again.
Fernando SotoRetiredCommented:
Is the issue solved?
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

kruegersteAuthor Commented:
kruegersteAuthor Commented:
correct, this worked perfectly, moving it into the Let statements as you suggested.

thanks again.
Fernando SotoRetiredCommented:
Not a problem, glad to help.  ;=)

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now