110,565
社区成员
发帖
与我相关
我的任务
分享
var componentItem = new Dictionary<int, List<string>>();
componentItem.Add(2, new List<string> {"aa", "bb", "cc"});
componentItem.Add(3, new List<string> { "dd", "bb", "ff" });
componentItem.Add(4, new List<string> { "ff", "cc", "nn" });
componentItem.Add(5, new List<string> { "ff", "tt", "rr" });
var newDic = new Dictionary<string, List<int>>();
foreach (var t in componentItem.Values)
{
t.ForEach(r =>
{
newDic[r] = componentItem.Where(m => m.Value.Contains(r)).Select(x => x.Key).ToList();
});
}
foreach (var x in newDic)
{
Console.WriteLine("{0} => {1}", x.Key, string.Join(", ", x.Value));
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace T392391892
{
class Program
{
static void Main(string[] args)
{
var componentItem = new Dictionary<int, List<string>>()
{
{ 2, new List<string> {"aa", "bb", "cc"}},
{ 3, new List<string> { "dd", "bb", "ff" }},
{ 4, new List<string> { "ff", "cc", "nn" }},
{ 5, new List<string> { "ff", "tt", "rr" }}
};
var query = componentItem.SelectMany(x => x.Value.Select(y => new { a = x.Key, b = y }))
.GroupBy(x => x.b)
.ToDictionary(x => x.Key, x => string.Join(",", x.Select(y => y.a)));
foreach (var item in query)
{
Console.WriteLine(item.Key + " has " + item.Value);
}
}
}
}
componentItem.Where(x => x.Value.Any(m => m == "cc"))
//创建查询表达式
var left = Expression.Parameter(typeof(string), "m");
var right = Expression.Constant("cc", typeof(string));
//通过上面的left和right构建出譬如m=="cc"的相等比较的表达式
var body = Expression.Equal(left, right);
//编译成表达式:m=>m=="cc"
var lambda = Expression.Lambda<Func<string, bool>>(body, left).Compile();
var results = componentItem.Where(d => d.Value.Any(lambda));
var componentItem = new Dictionary<int, List<string>>();
componentItem.Add(2, new List<string> {"aa", "bb", "cc"});
componentItem.Add(3, new List<string> { "dd", "bb", "ff" });
componentItem.Add(4, new List<string> { "ff", "cc", "nn" });
componentItem.Add(5, new List<string> { "ff", "tt", "rr" });
foreach (var x in componentItem.Where(t => t.Value.Contains("ff")))
{
Console.WriteLine(x.Key);
}