Czy istnieje sposób "LINQ" na warunkowy wybór danych, tj. Wybór z innego źródła, jeśli pierwszy był pusty? Jednym z przykładów jest sytuacja, gdy masz strukturę drzewiastą przedmiotów i chcesz uzyskać jakiś zasób z katalogu głównego, lub jeśli jest pusty, z jego elementów potomnych.Wybór warunkowy w LINQ (wybierz zamiast pustego)
Mam następujący przykład:
IEnumerable<Item> items = ...;
// Item has a Assets property that returns IEnumerable<Asset>
// Item has a SubItems property that returns IEnumerable<Item>
// i.e. other items with assets in them
// getting assets from a "main" item
var assets = item.Assets.Where(a => HasRelevantAsset(a));
// if there were no relevant assets in the "main" item
if (!assets.Any()) {
// then reselect from "subitems" assets instead
assets = item.SubItems.SelectMany(item =>
item.Assets.Where(a => HasRelevantAsset(a)));
}
// HasRelevantAsset(Asset) is a static method that returns
// true if it is the asset that is needed
Wydaje się, że jest to całkiem dobry użytek dla? operator - 'var assets = something ?? something_else'. To nie zadziała, ale byłoby miło, gdyby tak się stało. – zmbq