Ни одной строки LINQ, но это лучшее, что я мог придумать:
private List<String> GetCommonKeys(List<a> input, string[] users, string[] keyboardkeys)
{
var result = input.Where(c => users.Contains(c.userkey) && keyboardkeys.Contains(c.keyboardkey)).GroupBy (c => c.userkey);
var returnList = result.First().Select(c => c.keypressed);
foreach (var ls in result)
{
returnList = returnList.Intersect(ls.Select(t => t.keypressed));
}
return returnList.ToList();
}
struct a {
public String userkey;
public String keyboardkey;
public String keypressed;
}
Редактировать: как расширение:
public static class myExtensions
{
public static IEnumerable<TSelectType> SelectCommon<TSourceType, TSelectType>(this IEnumerable<IGrouping<TSelectType, TSourceType>> source, Func<TSourceType, TSelectType> action)
{
var firstResult = source.FirstOrDefault();
if(firstResult == null)
return new List<TSelectType>();
var returnCollection = firstResult.Select(action);
foreach(var ls in source)
returnCollection = returnCollection.Intersect(ls.Select(action));
return returnCollection;
}
}
Используется так:
var res = lst.Where(c => new[] {"u1", "u2", "u3"}.Contains(c.userkey) && c.keyboardkey == "kb1").GroupBy (c => c.userkey).SelectCommon(c => c.keypressed);