views:

633

answers:

4

Hi this is my code

var cityList = from country in doc.Element("result").Element("cities").Descendants("city")
                         select new
                         {
                             Name = country.Element("name").Value,
                             Code = country.Element("code").Value,
                             CountryCode = int.Parse(country.Element("countrycode").Value)
                         };


        foreach(var citee in cityList)
        {
        City city = new City();

        city.CountryID = from cnt in db.Countries where cnt.DOTWInternalID == citee.CountryCode select cnt.ID;
        }

Im getting an error on the second query as the title of this post, i tried converting to int, to nullable int nothing worked? help me guys

Thanks,

+1  A: 

As the error message says, your Linq query returns an System.Linq.IQueryable (for all intents and purposes a collection of ints). If you'd like to get one of them, you can either call First or ElementAt(n) to get the n'th element.

R0MANARMY
+2  A: 

it will return an iQueryable, you will need to do something like using the First

cit.CountryID = db.Countries.First(a=>a.DOTWInternalID == citee.CountryCode).ID
Pharabus
oops i was so dumb to ask this question, actually i didn't notice this, actually i was all this time concentrating on this cnt.DOTWInternalID == citee.CountryCode not the returning ID.. *sigh*
Aneef
Not dumb, I'm starting with linq and this answer was just what I needed! thanks
Paulo Manuel Santos
+2  A: 

IQueryable is not a single int - but a query that can represent a collection.

Maggie
+3  A: 

Here is the problem and solution

from cnt in db.Countries where cnt.DOTWInternalID == citee.CountryCode select cnt.ID part. If you omit the ID then it returns a Generic IEnumerable with Country(hoping that you have Country class) class. So what you have to do is first return the select criteria and select the first row then the ID field. Same like shown below.

cit.CountryID = (from cnt in db.Countries where cnt.DOTWInternalID == citee.CountryCode   select cnt).First<Country>().ID;

This will solve your problem.

Wonde