'Specified cast is not valid.linq query c#

I have a table name LibraryInfo with these columns

[libraryId] [smallint] IDENTITY(1,1) NOT NULL,
[libraryName] [nvarchar](200) NOT NULL,
[mId] [smallint] NOT NULL,
[description] [nvarchar](300) NULL,
[updatedBy] [int] NOT NULL,
[updatedAt] [datetime] NOT NULL

I have designed a dbml file to access this table and create a method as follows:

public List<LibraryInfo> GetLibraryInfos()
{

    var context = new BookDataClassesDataContext(){ ObjectTrackingEnabled = false };

    return context.LibraryInfos.ToList();
}

when I call this method 'return context.LibraryInfos.ToList();' shows me 'specified cast is not valid'.

Is there anyone to help me.



Solution 1:[1]

You probably changed one of the column data types in your table or view after importing the table/view into your linq model.

Just re-import it so the data types of the auto generated model are the same as the db structure.

Solution 2:[2]

I know years passed, but perhaps might be useful for someone else. As stated in previous answers, the problem is probably due to data type. My suggestion will be to check smallint conversion... I had a similar problem years ago, and later realized that querying tables with "int" data type causes the problem. I changed int to Int16 and the problem was solved. Not a best option, but in case you get the annoying error in cases like this one, I'd recommend to use int rather than smallint.

Solution 3:[3]

My former answer was obviously not correct. But:

You seem to have a mismatch of types here. ToList() on the context.LibraryInfos table would return a list of LibraryInfos objects (List<LibraryInfos>) instead of List<LibraryInfo>. So either your method needs to be declared as

public List<LibraryInfos> GetLibraryInfos()

or the code that returns the items needs to be changed to

return context.LibraryInfo.ToList();

This really depends on which one you want to return.


On your comment to your question:

context.LibraryInfo.ToList(); gives me error that dbml file does not contain a definition for LibraryInfo

Obviously you only have a LibraryInfos table in your data context. This means that you need to change your method return type to List<LibraryInfos>:

public List<LibraryInfos> GetLibraryInfos()

So the method now reads

public List<LibraryInfos> GetLibraryInfos()
{
    var context = new BookDataClassesDataContext(){ ObjectTrackingEnabled = false };
    return context.LibraryInfos.ToList();
}

Every item you get from the table has the type that matches the table name, as the LibraryInfos table is actually a collection of items of type LibraryItems.

You may have created a LibraryInfo class just because the compiler complained it didn't exist - you need to decide whether you can delete that from your project (look in solution explorer for LibraryInfo.cs file). But you can not cast between an item from the LibraryInfos table and that LibraryInfo class.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Markus
Solution 2 myteardrop4u
Solution 3