简体   繁体   中英

Mix in raw SQL into Entity Framework Select [C#]

I have a grid on a website that show a pre-select list a columns to users based on entity framework query

something like this

dbContext.Items.Select(i=> new {
    i.Name,
    i.description,
    ...
})

I want to give the user a option to have 1 custom column (its an internal system and i'm not worried about security) here is a sample what i would like to do

    dbContext.Items.Select(i=> new {
         i.Name,
         i.description,
         CustomColumn = dbContext.RawSQL("SELECT TOP 1 [CreatedDate] FROM [Sales] WHERE ItemId = " + i.Name)
     })

That's the concept i'm looking for, so i can let the user type his RAW-SQL and I will pass it into the select based on user.

I need raw SQL because its the most dynamic for my use case. And it will allow me to use tables there are no entities available (like views and SP)

And in the other hand I don't want to use full raw SQL because other than that column I fully use EF, and it way more than just this sample, and I don't want to lose the benefits that EF offers

i just came across this answer https://stackoverflow.com/a/7891426/4328664

looks like i will just need to implement a ExpressionVisitor class

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