LINQ and null values RRS feed

  • Question

  • Hi

    I have a question about the Contains function in LINQ and nullable values.

    I'm trying to do the following:



    static List<Campaign> GetCampaigns(int count, int sportTypeId)



    using (WebDataContext db = new WebDataContext())



    var relatedSportQuery = from relatedsport in db.RelatedSports


    where relatedsport.SportId == sportTypeId


    select relatedsport.RelatedSportId;


    var query = from camp in GetCampaigns()


    where relatedSportQuery.Contains(camp.SportId)


    select camp;


    return query.Take(count).ToList();



    I get an error at this line:

    where relatedSportQuery.Contains(camp.SportId)

    The error is:

    Error 2 Instance argument: cannot convert from 'System.Linq.IQueryable<int>' to 'System.Collections.Generic.IEnumerable<int?>' C:\Projects\Web\Web.DataAccess\Controllers\CampaignController.cs 118 33 Web.DataAccess

    It makes a bit sense to me as the value camp.SportId can be null from the database - but can I still use contains method or do I have to do something else to makes this comparison?

    Kind regards

    Wednesday, July 29, 2009 7:02 AM