【问题标题】:How to get value from IGrouping where key and value in object match如何从对象中的键和值匹配的 IGrouping 中获取值
【发布时间】:2021-06-23 20:04:02
【问题描述】:

我有一个包含三列的数据库表:CodeType、Code、Name

我将这些值保存在内存中,如果您愿意,我会将这些值分配给查找。

public class ShortCodeCache
{
    public DateTime LastRefresh { get; set; }

    public Lookup<string, ShortCode> CodeList { get; set; }
}

public class ShortCode
{
    public string CodeType { get; set; }
    public string Code { get; set; }
    public string Name { get; set; }
}

我从数据库中提取值并将它们分配给查找。

private static void GetShortCodeCache(DateTime current)
{
    try
    {
        using (SqlConnection cn = new MSSqlServerConnection().GetConnection())
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = Query;
                cmd.Connection = cn;
                cn.Open();

                List<ShortCode> codesList = new List<ShortCode>();

                SqlDataReader readline = cmd.ExecuteReader();
                while (readline.Read())
                {
                    ShortCode shortCode = new ShortCode
                    {
                        CodeType = readline["CodeType"].ToString().Trim(),
                        Code = readline["Code"].ToString().Trim(),
                        Name = readline["Name"].ToString().Trim()
                    };

                    codesList.Add(shortCode);
                }

                currentCache.CodeList = (Lookup<string, ShortCode>)codesList.ToLookup(code => code.CodeType);

                if (currentCache.CodeList.Count != 0)
                    currentCache.LastRefresh = current;

                if (log.IsDebugEnabled)
                {
                    log.Debug("LastCacheRefresh: " + currentCache.LastRefresh);
                }
            }
        }
    }
    catch (SqlException sqlex)
    {
        log.Error(sqlex.Message);
        throw;
    }
    catch (Exception ex)
    {
        log.Error(ex.Message);
        throw;
    }
        
    return; 
}

我相信这部分到这里为止是有效的。

我的问题是我无法获取特定 CodeType 和 Code 的名称。例如,我传入“DamageQuadrant”和“P9”,我需要 Name 值。

这是我目前拥有的,但 VS 对我咆哮说没有 CodeType 的定义。

public static string GetNameFromCode(string type, string code)
{
    CheckCodeCache();

    var codeType = currentCache.CodeList.Where(x => x.Key == type);
    string data = codeType.Where(x => x.CodeType, code).Name;

    return data;
}

我尝试了各种其他方法,包括在 Linq 代码中使用 && 条件。这看起来很容易,但到目前为止还不是。

【问题讨论】:

    标签: c# linq igrouping ilookup


    【解决方案1】:

    首先:您忘记将您的new ShortCodes 添加到codesList,因此当前代码不会在您的查找中为您提供任何信息。

    第二:使用ILookup&lt;&gt; 而不是Lookup&lt;&gt;。您没有充分的理由指定实现类型。如果您只希望每个组合有一个可能的项目,请改用 IDictionary&lt;&gt;

    第三:不要在查找或字典中使用.Where()。请改用[key] 语法。

    第四:在要查找的值上键入查找或字典。

    public IDictionary<(string Type, string Code), ShortCode> ShortCodesByTypeAndCode { get; set; }
    
    currentCache.ShortCodesByTypeAndCode = codesList.ToDictionary(code => (code.CodeType, code.Code));
    
    return currentCache.ShortCodesByTypeAndCode[(type, code)].Name;
    

    【讨论】:

    • 查找 [key] 会返回 IEnumerable&lt;&gt;.Name 不会编译)
    • Dictionary [key] 会为不存在的键抛出异常 :-) 需要 TryGetValue blah-blah。
    • 是的,取决于遇到无效代码时的预期行为。有时允许例外是最好的办法。
    猜你喜欢
    • 1970-01-01
    • 2013-12-03
    • 2012-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多