【问题标题】:Add to Dictionary Query Results of Multiple Linq Queries添加到多个 Linq 查询的字典查询结果
【发布时间】:2014-12-07 21:54:34
【问题描述】:

我想将 3 linq 的结果添加到单个字典中。

Dictionary<string, string> dict = new Dictionary<string, string>();        
dict = ctx.tbl1.Where(a => a.Id == cid).ToDictionary(a => a.FieldName, a => a.LabelName);                   
dict = ctx.tbl2.Where(a => a.Id == cid).ToDictionary(a => a.FieldName, a => a.LabelName);   
dict = ctx.tbl3.Where(a => a.Id == cid).ToDictionary(a => a.FieldName, a => a.LabelName);                    
return dict;

上面的代码给出了最后的dict 结果。如何将每个linq 查询结果添加到字典中?

【问题讨论】:

标签: c# linq dictionary


【解决方案1】:

您可以先创建您的 IEnumerable,然后最后将其转换为 Dictionary。

如果你的模型如下,

public class DummyModel
{
    private const decimal MinValue = 0m;
    public int Id { get; set; }
    public string FieldName { get; set; }
    public string LabelName { get; set; }

    //[DecimalValidatorAttrubute(precision: 2, maxdigits: 2, minValue: decimal.MinValue, maxValue: 99.99m)]
    //public decimal Decimal { get; set; }
}

下面的方法

private Dictionary<string, string> getDictionary()
    {
        var tbl1 = new List<DummyModel>() { new DummyModel() { Id = 1, FieldName = "Field1", LabelName = "Label1" }, new DummyModel() { FieldName = "Field2", LabelName = "Label2" } };
        var tbl2 = new List<DummyModel>() { new DummyModel() { Id = 1, FieldName = "Field3", LabelName = "Label3" }, new DummyModel() { FieldName = "Field4", LabelName = "Label4" } };
        var tbl3 = new List<DummyModel>() { new DummyModel() {Id = 1,FieldName = "Field5", LabelName = "Label5" }, new DummyModel() { FieldName = "Field6", LabelName = "Label6" } };
        const int cid = 1;

        Dictionary<string, string> dict = new Dictionary<string, string>();
        dict = tbl1.Where(a => a.Id == cid)
            .Concat(tbl2.Where(a => a.Id == cid))
            .Concat(tbl3.Where(a => a.Id == cid))
            .ToDictionary(a => a.FieldName, a => a.LabelName);
        return dict;
    }

会帮助你实现类似的目标

[TestMethod]
    public void LinqDictionary()
    {
        var dict = getDictionary();
        foreach (var item in dict)
        {
            Console.WriteLine("FieldName {0}, LabelName {1} ", item.Key, item.Value);
        }

    }

输出

FieldName Field1, LabelName Label1
FieldName Field3, LabelName Label3 
FieldName Field5, LabelName Label5

【讨论】:

    【解决方案2】:

    试试这个:

    Dictionary<string, string> dict = new Dictionary<string, string>();        
    foreach (var a in ctx.tbl1.Where(a => a.Id == cid)) dict.Add(a.FieldName, a.LabelName);
    foreach (var a in ctx.tbl2.Where(a => a.Id == cid)) dict.Add(a.FieldName, a.LabelName);
    foreach (var a in ctx.tbl3.Where(a => a.Id == cid)) dict.Add(a.FieldName, a.LabelName);
    return dict;
    

    不过有几点需要考虑:

    如果相同的(区分大小写的)a.FieldName 出现在多个表中或对于给定的a.Id 在同一个表中出现多次,则会抛出此错误。

    底层数据库是否有限制以防止这种情况发生?如果可能应该这样做,除非数据的性质使得一个a.FieldName 可以合法地针对给定的a.Id 出现多次。在后一种情况下,将它们映射到ILookup 可能更合适。

    如果a.FieldName 不区分大小写,则使用不区分大小写的字符串比较器创建字典。

    如果 a.Id,a.FieldName 的唯一性适用于 3 个表的聚合,那么将它们替换为单个表是否更有意义?

    【讨论】:

      【解决方案3】:

      要从多个查询中将项目添加到字典中,我使用了更多的 linq,直接将第一个集合添加到字典中,然后使用 tolist 和 foreach 添加后续项目。我怀疑性能是否超级好,但在这种情况下(添加控件)可能无关紧要,TBH 我怀疑 ToDictionary 无论如何执行得更快。

      Dictionary<string, SomeObject> controlsChanged;
      controlsChanged = this.Controls.OfType<TextBox>().Select(c => c.Name).ToDictionary(k => k, v => new SomeObject());
      this.Controls.OfType<ComboBox>().ToList().ForEach(c => controlsChanged.Add(c.Name, new SomeObject()));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-11-07
        • 2010-10-31
        • 2010-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-26
        相关资源
        最近更新 更多