Samantha J T Star
Samantha J T Star

Reputation: 32848

How can I cast a collection

This code:

IList<string> quids;
quids = db.Database.SqlQuery<string>("dbo.getById @Id",
                new SqlParameter { ParameterName = "Id", Value = 1 });

Produces the following error message:

Cannot implicitly convert type 'System.Data.Entity.Infrastructure.DbRawSqlQuery' to 'System.Collections.Generic.IList'.

An explicit conversion exists (are you missing a cast?)

Can someone explain to me how I can cast a collection?

Upvotes: 4

Views: 3632

Answers (1)

krimog
krimog

Reputation: 1276

IList<string> result = oldCollection.ToList();

DbRawSqlQuery<T> Does not implement the IList<T> interface (so, no direct cast possible). But it implements IEnumerable<T>, so you can call .ToList().

Upvotes: 8

Related Questions