I’ve got a linq query that returns one item.
When I do myList.Select(p => p.ID) it throws null reference exception, however, if I do myList.First().ID it’s fine.
I have already tried myList.ToList().Select(p => p.ID) and failed as well.
The most bizarre thing is that it works on another pc…
Any idea?
Code update
var test = A2012_DomainDB.GetGadgetDomainsForUser(userID);
var viewableGadgetIDs = test.Where(p => p != null).Select(p => p.GadgetID); // this line fails
public static IEnumerable<A2012_Domain_Gadget> GetGadgetDomainsForUser(int userID)
{
var db = Database_Factory.EVISION_EMAGINE_DB;
var viewableDomainIDs = GetDomainsForUser(userID).Select(p => p.DomainID);
var result = db.A2012_Domain_Gadget.Where(p => viewableDomainIDs.Contains(p.DomainID));
return result;
}
public static List<A2012_Domain> GetDomainsForUser(int userID)
{
var db = Database_Factory.EVISION_EMAGINE_DB;
List<int> viewableIDs = new List<int>();
List<A2012_Domain> domains = new List<A2012_Domain>();
viewableIDs.AddRange(db.A2012_Domain_User.Where(p => p.UserID == userID).Select(p => p.DomainID));
viewableIDs.ForEach(i =>
{
domains.Add(db.A2012_Domain.Where(p => p.DomainID == i).Single());
});
return domains;
}
I cannot really see exactly where this would come from. The
GetGadgetDomainsForUserquery should be deferred until you actually iterate the returnedviewableGadgetIDs, but you seem to imply the exception occurs when just running that line.All I can really suggest is to try null guarding all lambda expressions:
And:
I’d be most tempted to say the error is coming from here:
Perhaps the query into the
dbis yielding a null item, which is then used inp.DomainIDwithout a null check.