Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I cast a collection

Tags:

c#

collections

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?

like image 956
Samantha J T Star Avatar asked Feb 12 '23 15:02

Samantha J T Star


1 Answers

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().

like image 139
krimog Avatar answered Feb 15 '23 11:02

krimog