He llenado un DataGridView con una consulta LINQ que devuelve un tipo anónimo.DataGridVIew poblado con tipo anónimo, ¿cómo filtrar?
Pregunta: ¿hay alguna posibilidad de filtrar el DataGridView cuya fuente de datos es realmente anónima?
// Setting the datagridview data source
rawDocumentsDataGridView.DataSource = rawTopics
.SelectMany(t => t.Documents)
.Select(d => new
{
DocumentId = d.Id,
Rilevante = d.IsRelevant,
TopicId = d.Topic.Id // foreign key
}).ToList();
// Make it not visibile, waiting for master change
rawDocumentsDataGridView.Visible = false;
// When master selection changed...
void rawTopicsDataGridView_SelectionChanged(object sender, System.EventArgs e)
{
if (rawTopicsDataGridView.CurrentRow == null) return;
// Get selected topic id
int tid = (int) rawTopicsDataGridView.CurrentRow.Cells["TopicId"].Value;
// Filter rawDocumentsDataGridView based on topic id
// WARNING: PSEUDO CODE
var oldDataSource = (List<AnonymousType>)rawDocumentsDataGridView.DataSource;
rawDocumentsDataGridView.DataSource = oldDataSource
.Where(d => d.TopicId == tid);
}
Sí que estaba equivocado, yo iba a filtrar los datos ya filtrados. Está bien. Pero me gustaría saber cómo hacerlo con tipos anónimos de todos modos ... – gremo
@Gremo - there * is * un truco llamado cast-por-ejemplo, pero realmente no vale la pena. Seriamente; no use un tipo anónimo aquí. Solo tienen sentido cuando los datos se producen y se consumen en el mismo contexto (o solo * se usan por elementos como la reflexión). Este no es el caso aquí. También podría hacer el filtrado mediante reflexión, pero eso es lento * y * frágil. –
Bien, voy a exponer el campo. Estoy de acuerdo contigo sobre la reflexión ... no vale ... – gremo