LINQ to hash - 无法正确进行转换!
我在 SQL Server 中有一个表,其中包含 3 列、一个域、一个键和一个值。我想最终得到一个可以在 dot-net 中使用的哈希值。我正在使用 EF 4.0。
起初我尝试过:
List<Dictionary<string, string>> myHash = db.Registries
.Where(x => x.Domain == "Pricing")
.Select(x => new { Key = x.Key, Value = x.Value });
抱怨:
错误 3 无法将类型“System.Linq.IQueryable”隐式转换为“System.Collections.Generic.List>”。存在显式转换(是否缺少转换?) C:\Users\ekkis\Documents\Visual Studio 2010\Projects\SkillScore\Website\Controllers\HomeController.cs 53 21 Website
因此显然需要调整选择的类型。我已经搞砸了半个小时(我太没用了!)却无法让它工作(天哪,我想念 IRC)。帮助任何人吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来这就是您所需要的:
这为每个域创建一个单独的字典并将它们全部放在一个列表中。但这并不完全有用,因为您丢失了有关域名的信息 - 也许
Dictionary>
会更符合您的需求(将域名设置为外部字典的键)。您可以直接使用
ToDictionary()
获取单个域的字典:请注意,pricingDictionary 的类型为
Dictionary
,因此如果您需要完整类型,您可以可以做:Sounds like this is what you need:
This creates a separate dictionary for each domain and puts them all in a list. This is not entirely useful though, since you lose the information about the domain name - maybe a
Dictionary<string, Dictionary<string,string>>
would be more what you want (having the domain name as key of the outer dictionary).A dictionary for a single domain you can get by using
ToDictionary()
directly:Note that pricingDictionary is of type
Dictionary<string, string>
so if you need the full type you can do:我对“如何在 LINQ 查询中构建字典的字典”感兴趣。
这是我的卑微尝试,它未经测试,因为我没有 LINQ-DB,因为我没有 IIS,因为我无法安装 IIS,因为某些人可能会猜到“真正”的原因。
请随意编辑此代码来修复它(如果它已损坏)。
请原谅我:这些查询只是增加了我的观点的可信度,即 LINQ 太丑了,它的后部应该被剃掉,并且应该教它倒着走。确实:“长手”版本更加冗长,但它也更加不言自明。我想我的观点是“LINQ 很伟大。对 STEEEEEEP 学习曲线感到羞耻”。
并且,是的,请...让我们回到 Perl cgi 脚本。上帝保佑我,父亲,因为我不能继承……我爱上了一个平民。叹。
干杯。基思.
I got interested in: "How to build a dictionary-of-dictionaries in a LINQ query".
Here's my humble attempt, which is untested because I don't a LINQ-DB, because I don't have IIS, because I can't intall IIS for "Genuine" reasons some might guess.
Please feel free to edit this code to fix it (if it's broke).
And Forgive me: These queries just add credence to my opinion that LINQ is sooooo ugly that it's posteriour should be shaved, and it should be taught to walk backwards. True: the "long-hand" version IS a LOT more verbose, BUT it's also a LOT more self-explanatory. I suppose my perspective is that "LINQ is grand. Shame about the STEEEEEEP learning curve".
And, yes please... lets all go back to Perl cgi-scripts. Bless me father, for I cannot inheret... I'm in love with a commoner. Sigh.
Cheers. Keith.