LINQ to hash - 无法正确进行转换!

发布于 2024-11-04 15:21:58 字数 622 浏览 1 评论 0 原文

我在 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)。帮助任何人吗?

I have a table in SQL Server with 3 columns, a domain, a key and a value. I'd like to end up with a hash that I can use in dot-net. I'm using EF 4.0.

at first I tried:

List<Dictionary<string, string>> myHash = db.Registries
  .Where(x => x.Domain == "Pricing")
  .Select(x => new { Key = x.Key, Value = x.Value });

which complains:

Error 3 Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List>'. An explicit conversion exists (are you missing a cast?) C:\Users\ekkis\Documents\Visual Studio 2010\Projects\SkillScore\Website\Controllers\HomeController.cs 53 21 Website

so obviously the type of the select needs to be tweaked. I've messed with it for half an hour (I'm so useless!) without being able to make it work (God I miss IRC). help anyone?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一世旳自豪 2024-11-11 15:21:58

听起来这就是您所需要的:

List<Dictionary<string, string>> myHash = db.Registries
                                            .GroupBy(x => x.Domain)
                                            .Select(g => g.ToDictionary(x => x.Key, 
                                                                        x => x.Value))
                                            .ToList() ;

这为每个域创建一个单独的字典并将它们全部放在一个列表中。但这并不完全有用,因为您丢失了有关域名的信息 - 也许 Dictionary> 会更符合您的需求(将域名设置为外部字典的键)。

您可以直接使用 ToDictionary() 获取单个域的字典:

var pricingDictionary = db.Registries
                          .Where(x => x.Domain == "Pricing")
                          .ToDictionary( x => x.Key, 
                                         x => x.Value);

请注意,pricingDictionary 的类型为 Dictionary,因此如果您需要完整类型,您可以可以做:

Dictionary<string, string> myHash = db.Registries
                                      .Where(x => x.Domain == "Pricing")
                                      .ToDictionary( x => x.Key, 
                                                     x => x.Value);

Sounds like this is what you need:

List<Dictionary<string, string>> myHash = db.Registries
                                            .GroupBy(x => x.Domain)
                                            .Select(g => g.ToDictionary(x => x.Key, 
                                                                        x => x.Value))
                                            .ToList() ;

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:

var pricingDictionary = db.Registries
                          .Where(x => x.Domain == "Pricing")
                          .ToDictionary( x => x.Key, 
                                         x => x.Value);

Note that pricingDictionary is of type Dictionary<string, string> so if you need the full type you can do:

Dictionary<string, string> myHash = db.Registries
                                      .Where(x => x.Domain == "Pricing")
                                      .ToDictionary( x => x.Key, 
                                                     x => x.Value);
拔了角的鹿 2024-11-11 15:21:58

我对“如何在 LINQ 查询中构建字典的字典”感兴趣。

这是我的卑微尝试,它未经测试,因为我没有 LINQ-DB,因为我没有 IIS,因为我无法安装 IIS,因为某些人可能会猜到“真正”的原因。

请随意编辑此代码来修复它(如果它已损坏)。

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args) {
            var registries = FetchAllRegistries();
            foreach ( KeyValuePair<string, Dictionary<string,string>> domain in registries ) {
                Console.WriteLine("Domain: "+domain.Key);
                foreach ( KeyValuePair<string,string> entry in domain.Value ) {
                    Console.WriteLine("  "+entry.Key+" "+entry.Value);
                }                
            }
        }

        private static Dictionary<string, Dictionary<string, string>> FetchAllRegistries() {
            return db.Registries
                .GroupBy(x => x.Domain)
                .Select(g => g.ToDictionary(
                    d => d.Domain,
                    d => d.ToDictionary(
                        kv => kv.Key,
                        kv => kv.Value
                    )
                ))
            ;
        }

        private static Dictionary<string, Dictionary<string, string>> FetchRegistry(string domainName) {
            return db.Registries
                .Where(x => x.Domain == domainName)
                .ToDictionary(
                    x => x.Key
                  , x => x.Value
                );
        }

    }

}

请原谅我:这些查询只是增加了我的观点的可信度,即 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).

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args) {
            var registries = FetchAllRegistries();
            foreach ( KeyValuePair<string, Dictionary<string,string>> domain in registries ) {
                Console.WriteLine("Domain: "+domain.Key);
                foreach ( KeyValuePair<string,string> entry in domain.Value ) {
                    Console.WriteLine("  "+entry.Key+" "+entry.Value);
                }                
            }
        }

        private static Dictionary<string, Dictionary<string, string>> FetchAllRegistries() {
            return db.Registries
                .GroupBy(x => x.Domain)
                .Select(g => g.ToDictionary(
                    d => d.Domain,
                    d => d.ToDictionary(
                        kv => kv.Key,
                        kv => kv.Value
                    )
                ))
            ;
        }

        private static Dictionary<string, Dictionary<string, string>> FetchRegistry(string domainName) {
            return db.Registries
                .Where(x => x.Domain == domainName)
                .ToDictionary(
                    x => x.Key
                  , x => x.Value
                );
        }

    }

}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文