I have a class that returns a list of Regions in a database using Linq to SQL (passing a RegionName and RegionID variable). The issues comes when I am trying to fetch the data within. I can see the values when I am debugging the code (come out looking like this: {RegionID = 1, RegionName = “Asia”}). That is also when using a foreach loop to look at each item/row that was returned.
Any ideas on how to break these down so I can grab the values for each?
Here is the LINQ statement that is being used:
return (from r in db.Regions
join c in db.Countries on r.RegionID equals c.RegionId
join i in db.Programs on c.CountryID equals i.CountryID
select new { r.RegionID, r.RegionName })
.Distinct();
If you are returning your resulting sequence from one method to another, go ahead and define a proper type.
And modify your query to select RegionData
And your method should return
IEnumerable<RegionData>.This will give your compile time access to the properties when you need to iterate over the sequence elsewhere in your code. Anonymous types are useful, but when you start passing them around, it’s time to give turn them into a defined type.