What is the best way to cast each item in a LINQ to Entities query to an interface?

两盒软妹~` 提交于 2020-01-01 09:23:22

问题


I have an entity object 'User' which implements 'IUser':

IQueryable<User> users = Db.User;
return users;

But what I actually want to return is:

IQueryable<IUser>

So what is the best way to convert

IQueryable<User>

to

IQueryable<IUser>

without actually running the query? Right now I am doing this but it seems like a hack:

IQueryable<IUser> users = Db.User.Select<User, IUser>(u => u);

回答1:


Your "hacky" solution looks fine to me.



来源:https://stackoverflow.com/questions/1240087/what-is-the-best-way-to-cast-each-item-in-a-linq-to-entities-query-to-an-interfa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!