Ramy Atef

Ramy Atef

  • NA
  • 58
  • 498

C# stored procedure with dynamic select values an Calling using Entity

Feb 3 2021 9:08 AM
I want to make a stored procedure to filter by passing columns and conditions in run time
t use this code this run but when i calling by Entity FrameWork must passing all columns in table to run but I do not like this i want to select a column different every executes how do this?
```ALTER proc [dbo].[spDynamicFilteration](
@ColumnsNames nvarchar(MAX),
@Conditions nvarchar(MAX)=null)
as
begin
EXEC ('select '+@ColumnsNames+' from TableName where ' +@Conditions+' ')
return
end ```
Calling by EF i used this
``` string data = "Id,TunnelId,VehicleTypedId,CategoryId,TotalCash,VisaCount,TunnelId,ShiftId,Date,LaneId,TotalCash,CashCount,TotalVisa,VisaCount";
string a = "ShiftId = 1 And Id = 78";
var d = entities.Filteration(data, "Id = " + a).FirstOrDefault();```
and this ` var orders = entities.Database.SqlQuery<PaidReport>("EXEC spDynamicFilteration {0},{1}", data, a).FirstOrDefault();
`
but stale same problem required all columns in table pass to run

Answers (7)