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

.net c# entity-framework-6 sql sql-server

Question

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

1
0
7/20/2017 8:29:13 PM

Popular Answer

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

looks like i will just need to implement a ExpressionVisitor class

0
7/20/2017 8:34:59 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow