I'm converting some inline SQL code over to Entity Framework Core 6. The overall application is in C#. Some of the existing code has SQL that can take an optional WHERE
clause.
A simplified example might look like this:
public List<DataObject> SelectWithWhere(string optionalWhere)
{
string sql = string.Format("SELECT * FROM SomeTable {0} ;", optionalWhere);
// call to DB class to execute query and format into List (pseudo code)
List<DataObject> list = ExecuteSqlAndFormat(sql);
return list;
}
I'd like to be able to pass in Where criteria to a similar function that uses Entity Framework Core 6, but have not been able to find any good clear examples of passing a where clause, or some kind of entity structure that represents a where clause, into a method that can be then passed to EF Core.
public List<DataObject> SelectWithWhere(string optionalWhere)
{
List<DataObject> list = (from t in dbContext.SomeTable.Where(-- what to do here? --)
select new DataObject
{
// fill in data members from query
}
).ToList<DataObject>()
return list;
}
The preferred solution would use only what is available in Entity Framework Core 6 and not any third party add-ons or proprietary tools.
Thanks!