tags:

views:

81

answers:

4
 public List<NDT_Equipment> GetALLRecords()
    {
        using (NDT_DB)
        {
            return (from a in NDT_DB.NDTEquipment select a);
        }
    }
  • where : NDT_DB - class instance of the autogenerated database context class NDT_Equipment - partial class inside the database context class

  • what I want :

    • return all records from table NDTEquipment
  • what Im getting : " are you missing a cast " error everytime I compile ..

question : - am I allowed to use the NDT_Equipment partial class as a return value to be used in my VIEWMODEL ??? or I need to create a specific class to contain the results from this LINQ statement and that class is to be used in my VIEWMODEL ?

+7  A: 

Try this:

return (from a in NDT_DB.NDTEquipment select a).ToList();
Steven
Cannot implicitly convert type System.Collections.Generic.List<NDT.Models.DataAccess.NDTEquipment>' to System.Collections.Generic.List<NDT.Models.DataAccess.NDT_Equipment
Dennis
Seems you have two different classes then, NDTEquipment and NDT_Equipment.
Anders Fjeldstad
Anders .. you ARE RIGHT .. its my stupidity .. i have another class defined as NDT_Equipment ( which intelisense offered firts when I was codin) and the class I wanted which is NDTEquipment (without the underscore) thanks !!!
Dennis
+1  A: 

I think you're getting the error because the LINQ statement doesn't return a List (which your method signature states). Try changing this line:

return (from a in NDT_DB.NDTEquipment select a);

To:

return (from a in NDT_DB.NDTEquipment select a).ToList();
Anders Fjeldstad
Dup of http://stackoverflow.com/questions/2200445/using-linq-inside-a-function/2200468#2200468
Ruben Bartelink
@Ruben Bartelink - In SO, we have problems with duplicate questions, not answers.
Oded
@Oded: I agree, but in this case, we have 3 answers that a) are valid responses b) are the same c) arent the solution because the questioner messed up. I fail to see what the two misleading duplicates add. But I understand how hard it is to delete a duplicate once one has had an upvote.
Ruben Bartelink
+1  A: 

The LINQ query is returning an IQueryable, not a list, so you need to convert it to a list first:

return (from a in NDT_DB.NDTEquipment select a).ToList();
Oded
Dup of http://stackoverflow.com/questions/2200445/using-linq-inside-a-function/2200468#2200468
Ruben Bartelink
+1  A: 

If you don't really need to use the features provided by List<NDT_Equipment> I would change the signature of the method to return IEnumerable<NDT_Equipment> instead:

public IEnumerable<NDT_Equipment> GetALLRecords()
{
    using (NDT_DB)
    {
        return (from a in NDT_DB.NDTEquipment select a);
    }
}

Update
There seems to be something strange in the code though; the generic type argument in the method is NDT_Equipment while the data entity is called NDTEquipment; are you specifying the correct type argument? If you point at a in your code, what type is it reported to be? That is the type you should have in the generic type argument.

Fredrik Mörk
- im getting "are you missing a cast" error if I changed it to IEnumerable
Dennis
@Dennis: see my updated answer. (edit: I see now that @Anders picked up that as well; will leave my answer though, as an alternative solution)
Fredrik Mörk