Dynamically selecting columns for EF Query

65 Views Asked by At

**Disclaim: The provided scenario is made up scenario, and may not be aligned properly. But as you read pseudocode further it will make sense. Please don't discourage people like me by giving negative comment, as there are so many people in the world who doesn't know how to ask a question, just because they have just started learning to unitize new technical solution.

Here is my question with scenario

I am tying to supply name of columns dynamically, that comes from various relational tables, and supply it to ".Select" of EF query below.

I am also utilizing FromSQLRaw of EF to achieve dynamic query, due to complexity. rawSql gets built dynamically with very complex joins and conditional logics.

`rawSQL = "SELECT Inv.* FROM Invoice Inv LEFT JOIN Product P ON P.InvoiceID = Inv.ID " bla bla bla'

I have a list of column names which comes dynamically as well, and I wanted to supply that using some variable something like below.

  selectedColumn = "sales.item, product.expiryDate, inventory.qtyStock ..."

  var result = myContext.products
              .FromSqlRaw(rawSQL.ToString())
              .Select( selectedColumn ) //this where where I want to pass columns name
              .ToList();
0

There are 0 best solutions below