I have a queryable which is returning a table with 4 columns:
ItemName | Country A | Country B | Country C |
I have some authorization processes in my app and some users should see only specific country columns.
For example, user with claim country=A should only see:
ItemName | Country A |
Is there a way to filter the columns based on the name of the column?
IQueryable<AllCountries> ResultsVM = _context.AllCountries;
var country = IdentityExtensions.GetCountryId(User);
if (country != null)
{
Type elementType = ResultsVM.ElementType;
foreach (PropertyInfo pi in elementType.GetProperties())
{
if (country==pi.Name)
{
ResultsVM= _context.AllCountries
.Select(t => new AllCountries
{
ItemName=t.ItemName,
?? only the column which matches the country
});
}
}
}
So far, I know when a column of my IQueryable corresponds to the country of the user but I don't know in my Select how to show it and remove the others...
Update
My ViewModel ResultsVM which stores the IQueryable and will be used in my View in my MVC web project, is defined as a list of:
public class AllCountries
{
[DisplayName("Item")]
public string ItemName { get; set; }
public int? Country1 { get; set; }
public int? Country2 { get; set; }
public int? Country3 { get; set; }
}
Thank you
Sylvain