简体   繁体   中英

Entity Framework SqlQuery returing anonymous type records

I am writing a reporting system which is flexible. As a part of it I create SQl statements by concatenating like

sql=" select * from a_v where ename=1 "

I want to know How I can use

db.Database.SqlQuery(sql)

to return collection of anonymous records so it is truly flexible.

I can't find a way to do it as it seems to be strongly typed.

Can it be made to return anonymous type records . An example would be great

I may have interpreted your goal incorrectly, but it sounds to me that you want to use projections. You can return lists of anonymous types from Linq. An example would be:

var anonymousListOfBoxes =
   from b in CustomerBoxes
   select new {
        b.Customer,
        b.BoxID,
        b.Barcode
   };

now, anonymousListOfBoxes will be a System.Linq.IQueryable<(anonymous)>. Each item will have 3 properties - Customer, BoxID and Barcode. The list will not be a collection of CustomerBoxes as it would be if you did not use a projection with "select".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM