jeudi 13 avril 2023

Filter some columns from IQueryable in c#

I need to remove some columns from an IQueryable that include another object using reflection (so i don't want to use anonymous type for create the new IQueryable without a columns).

I've tried this but this isn't the right solution:

    string[] columnsToRemove = { "Insurance" };
    var myQuery = _dataService.GetQuery<Insurance>().Include(i => i.Partner);

    var attributes = myQuery.GetType().GetProperties();

    foreach (var attribute in attributes)
    {
        foreach (var column in columnsToRemove)
        {
            if (attribute.Name != column)
            {
                // Remove the column from query
                var newQuery = myQuery.Where(t => t.GetType().GetProperty(column).Name != column);
                return newQuery;
            }
        }
    }




Aucun commentaire:

Enregistrer un commentaire