Dictionary 使用问题求助?

jbhou 2018-06-04 06:28:54
我有一个字典对象, 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" });


如果我想获取包含"bb"的字典key, 应该是要取出 2和3,如果我想获取包含"ff"的字典Key,应该是要取出,3,4,5。

请问大神们有没有遇到过这种场景,如果实现,谢谢。

...全文
703 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzuning 2018-06-05
  • 打赏
  • 举报
回复
            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));
}
threenewbee 2018-06-05
  • 打赏
  • 举报
回复
或者你要取cc query["cc"] 就是
threenewbee 2018-06-05
  • 打赏
  • 举报
回复
aa has 2 bb has 2,3 cc has 2,4 dd has 3 ff has 3,4,5 nn has 4 tt has 5 rr has 5 Press any key to continue . . .
threenewbee 2018-06-05
  • 打赏
  • 举报
回复
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);
            }
        }
    }
}
正怒月神 2018-06-05
  • 打赏
  • 举报
回复
不清楚楼主到底说的什么意思。 #1的方法就好了。你要传递参数,就把"ff"拿出来,作为变量传参就可以了。 什么"ff","bb"等等,都是一样的
  • 打赏
  • 举报
回复
提到动态生成,又恰好是集合操作,所以很自然会想到Linq表达式
先梳理下,按照正常逻辑需要构建以下的语句:
componentItem.Where(x => x.Value.Any(m => m == "cc"))

按照你的需求,你需要动态传递“cc”这个值,故而只需要构建出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));

因为Any需要的委托类型为Func<string, bool>

最后你只需要将代码的1-8行封装成方法,将right 中的“cc”作为参数传递即可。
vivismilecs 2018-06-05
  • 打赏
  • 举报
回复
很有帮助,顶一下
jbhou 2018-06-05
  • 打赏
  • 举报
回复
caozhy 和 xuzuning 的回复就是我说的需求,非常感谢 caozhy 和 xuzuning。另外,也非常感谢其他朋友的积极回复。
此生得你 2018-06-05
  • 打赏
  • 举报
回复
用for循环挨个比较
xuzuning 2018-06-05
  • 打赏
  • 举报
回复
主贴确实没有说清诉求(而不是可能),在 #4 补充了 你可以像 #6 那样使用 SelectMany,但我觉得才是分开写更直观。至多在向 newDic 赋值时做下判断(键已存在就不赋值)
CyberLogix 2018-06-05
  • 打赏
  • 举报
回复
引用 1 楼 xuzuning 的回复:
            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);
            }
这个方法不错啊
jbhou 2018-06-04
  • 打赏
  • 举报
回复
可能是我没表达清楚,我意思是,变量不是从外面传递过来的。 就是基于现有的数据结构形成新的数据结构,比如形成下面这样: var newDic = new Dictionary<string, List<int>>(); newDic.Add("bb", new List<int> { 2, 3 }); newDic.Add("cc", new List<int> { 2, 4 }); newDic.Add("ff", new List<int> { 3, 4, 5 }); 不知道我这样描述清楚了吗? 谢谢。
xuzuning 2018-06-04
  • 打赏
  • 举报
回复
把字符串常量改写成变量都不会???
jbhou 2018-06-04
  • 打赏
  • 举报
回复
谢谢xuzuning 的回复,不过我是要实现动态的方式,"ff"不能写死,也可能是取"cc"。我只是举个例子,实际数据会很多。
xuzuning 2018-06-04
  • 打赏
  • 举报
回复
            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);
}
trade_create_by_buyer-CSHARP-UTF-8 │ ├app_code ┈┈┈┈┈┈┈┈┈┈类文件夹 │ │ │ ├AlipayConfig.cs┈┈┈┈┈基础配置类文件 │ │ │ ├AlipayCore.cs┈┈┈┈┈┈支付宝接口公用函数类文件 │ │ │ ├AlipayNotify.cs┈┈┈┈┈支付宝通知处理类文件 │ │ │ ├AlipaySubmit.cs┈┈┈┈┈支付宝各接口请求提交类文件 │ │ │ └MD5.cs ┈┈┈┈┈┈┈┈┈MD5类库 │ ├log┈┈┈┈┈┈┈┈┈┈┈┈┈日志文件夹 │ ├default.aspx ┈┈┈┈┈┈┈┈支付宝接口入口文件 ├default.aspx.cs┈┈┈┈┈┈┈支付宝接口入口文件 │ ├notify_url.aspx┈┈┈┈┈┈┈服务器异步通知页面文件 ├notify_url.aspx.cs ┈┈┈┈┈服务器异步通知页面文件 │ ├return_url.aspx┈┈┈┈┈┈┈页面跳转同步通知文件 ├return_url.aspx.cs ┈┈┈┈┈页面跳转同步通知文件 │ ├Web.Config ┈┈┈┈┈┈┈┈┈配置文件(集成时删除) │ └readme.txt ┈┈┈┈┈┈┈┈┈使用说明文本 ※注意※ 需要配置的文件是: alipay_config.cs default.aspx default.aspx.csreturn_url.aspx return_url.aspx.cs notify_url.aspx notify_url.aspx.cs统一命名空间为:namespace Com.Alipiay ───────── 类文件函数结构 ───────── AlipayCore.cs public static Dictionary ParaFilter(SortedDictionary dicArrayPre) 功能:除去数组中的空值和签名参数并以字母a到z的顺序排序 输入:SortedDictionary dicArrayPre 过滤前的参数组 输出:Dictionary 去掉空值与签名参数后的新签名参数组 public static string CreateLinkString(Dictionary dicArray) 功能:把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串 输入:Dictionary dicArray 需要拼接的数组 输出:string 拼接完成以后的字符串 public static string CreateLinkStringUrlencode(Dictionary dicArray, Encoding code) 功能:把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串,并对参数值做urlencode 输入:Dictionary dicArray 需要拼接的数组 Encoding code 字符编码 输出:string 拼接完成以后的字符串 public static void log_result(string sPath, string sWord) 功能:写日志,方便测试(看网站需求,也可以改成存入数据库) 输入:string sPath 日志的本地绝对路径 string sWord 要写入日志里的文本内容 public static string GetAbstractToMD5(Stream sFile) 功能:获取文件的md5摘要 输入:Stream sFile 文件流 输出:string MD5摘要结果 public static string GetAbstractToMD5(byte[] dataFile) 功能:获取文件的md5摘要 输入:byte[] dataFile 文件流 输出:string MD5摘要结果 ┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉ MD5.cs public static string Sign(string prestr, string key, string _input_charset) 功能:签名字符串 输入:string prestr 需要签名的字符串 string key 密钥 string _input_charset 编码格式 输出:string 签名结果 public static bool Verify(string prestr, string sign, string key, string _input_charset) 功能:验证签名 输入:string prestr 需要签名的字符串 string sign 签名结果 string key 密钥 string _input_charset 编码格式 输出:string 验证结果 ┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉ AlipayNotify.cs public Notify() 功能:构造函数 从配置文件中初始化变量 public bool Verify(SortedDictionary inputPara, string notify_id, string sign) 功能:验证消息是否是支付宝发出的合法消息 输入:SortedDictionary inputPara 通知返回参数数组 string notify_id 通知验证ID string sign 支付宝生成的签名结果 输出:bool 验证结果 private string GetPreSignStr(SortedDictionary inputPara) 功能:获取待签名字符串(调试用) 输入:SortedDictionary inputPara 通知返回参数数组 输出:string 待签名字符串 private bool GetSignVeryfy(SortedDictionary inputPara, string sign) 功能:获取返回回来的待签名数组签名后结果 输入:SortedDictionary inputPara 通知返回参数数组 string sign 支付宝生成的签名结果 输出:bool 签名验证结果 private string GetResponseTxt(string notify_id) 功能:获取是否是支付宝服务器发来的请求的验证结果 输入:string notify_id 通知验证ID 输出:string 验证结果 private string Get_Http(string strUrl, int timeout) 功能:获取远程服务器ATN结果 输入:string strUrl 指定URL路径地址 int timeout 超时时间设置 输出:string 服务器ATN结果字符串 ┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉ AlipaySubmit.cs private static string BuildRequestMysign(Dictionary sPara) 功能:生成签名结果 输入:Dictionary sPara 要签名的数组 输出:string 签名结果字符串 private static Dictionary BuildRequestPara(SortedDictionary sParaTemp) 功能:生成要请求给支付宝的参数数组 输入:SortedDictionary sParaTemp 请求前的参数数组 输出:Dictionary 要请求的参数数组 private static string BuildRequestParaToString(SortedDictionary sParaTemp, Encoding code) 功能:生成要请求给支付宝的参数数组 输入:SortedDictionary sParaTemp 请求前的参数数组 Encoding code 字符编码 输出:string 要请求的参数数组字符串 public static string BuildRequest(SortedDictionary sParaTemp, string strMethod, string strButtonValue) 功能:建立请求,以表单HTML形式构造(默认) 输入:SortedDictionary sParaTemp 请求参数数组 string strMethod 提交方式。两个值可选:post、get string strButtonValue 确认按钮显示文字 输出:string 提交表单HTML文本 public static string BuildRequest(SortedDictionary sParaTemp) 功能:建立请求,以模拟远程HTTP的POST请求方式构造并获取支付宝的处理结果 输入:SortedDictionary sParaTemp 请求参数数组 输出:string 支付宝处理结果 public static string BuildRequest(SortedDictionary sParaTemp, string strMethod, string fileName, byte[] data, string contentType, int lengthFile) 功能:建立请求,以模拟远程HTTP的POST请求方式构造并获取支付宝的处理结果 输入:SortedDictionary sParaTemp 请求参数数组 string strMethod 提交方式。两个值可选:post、get string fileName 文件绝对路径 byte[] data 文件数据 string contentType 文件内容类型 int lengthFile 文件长度 输出:string 支付宝处理结果 public static string Query_timestamp() 功能:用于防钓鱼,调用接口query_timestamp来获取时间戳的处理函数 输出:string 时间戳字符串 ────────── 出现问题求助方法 ────────── 如果在集成支付宝接口时,有疑问或出现问题,可使用下面的链接,提交申请。 https://b.alipay.com/support/helperApply.htm?action=supportHome 我们会有专门的技术支持人员为您处理

110,565

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧