Код:
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp
{
class Program
{
public class Word
{
public Dictionary<string, string> Langs;
}
static void Main(string[] args)
{
List<string> langsToUse = new List<string> { "en", "pl" };
List<Word> wordsList = new List<Word> {
new Word {
Langs = new Dictionary<string, string> {
{"en", "Dog"},
{"pl", "Pies"},
{"ge", "Hund"},
//... and so on
}},
new Word {
Langs = new Dictionary<string, string> {
{"en", "Kat"},
{"pl", ""},
{"ge", ""}
//... and so on
}},
new Word {
Langs = new Dictionary<string, string> {
{"en", "Car"},
{"pl", ""},
{"ge", ""}
//... and so on
}},
};
// LETS START
// select all words with condition
var words = wordsList.Where(w => true);
foreach (var word in words)
{
// select langs where langsToUse contains it
var langs = word.Langs.Where(l => langsToUse.Contains(l.Key));
// show in console
Console.WriteLine("Langs in langsToUse:");
langs.ToList().ForEach(l => Console.WriteLine("{0} -> {1}", l.Key, l.Value));
// is all values in selection are not epty?
bool isValid = langs.All(l => l.Value != "");
// show in console
Console.WriteLine("All not Empty, method one: {0}", isValid);
// OR another method
// is any value contains empty string?
bool containsEmptyStr = langs.Any(l => l.Value == "");
isValid = !containsEmptyStr;
Console.WriteLine("All not Empty, method two: {0}", isValid);
// add some formatting
Console.WriteLine("\n");
}
// COMBINE
var selected = wordsList.Where(
word => word.Langs
.Where(lang => langsToUse.Contains(lang.Key))
.All(l => l.Value != ""));
// show in console:
Console.WriteLine("Combined method:");
selected.ToList()
.ForEach(w => w.Langs.ToList()
.ForEach(l => Console.WriteLine("{0} -> {1}", l.Key, l.Value)));
Console.ReadKey();
}
}
}
Вывод:
Langs in langsToUse:
en -> Dog
pl -> Pies
All not Empty, method one: True
All not Empty, method two: True
Langs in langsToUse:
en -> Kat
pl ->
All not Empty, method one: False
All not Empty, method two: False
Langs in langsToUse:
en -> Car
pl ->
All not Empty, method one: False
All not Empty, method two: False
Combined method:
en -> Dog
pl -> Pies
ge -> Hund
Это конечно всё интересно, но код нечитаемый получается. Используйте лучше обычные циклы или раздельный вариант запросов.