【问题标题】:join two class list to another list将两个班级列表加入另一个列表
【发布时间】:2019-10-16 12:06:42
【问题描述】:

我有 2 个对象:ParentOne 和 ParentTwo:

class B
    {
        public int id { get; set; }
        public string AtterC { get; set; }
        public string AtterE { get; set; }
        public int AtterG { get; set; }
    }

    class ParentOne
    {
        public string AttA { get; set; }
        public List<B> AttrBList { get; set; }
    }

    //-----------
    class ParentTwo 
    {
        public List<C> AttrCList { get; set; }
    }
    class C
    {
        public int id { get; set; }
        public string name { get; set; }
        public int AtterF { get; set; }
    }

我想加入两个带有 id 的列表类

ParentOne parent1 = get(..)
ParentTwo parent2 = get(..)

     var query = parent1.AttrCList.Join(
            parent2.AttrBList,
            l1 => new { l1.id },
            l2 => new { l2.id },
            (item1, item2) => new
            {
                id= item1.id,
                name = item2.name,
                AtterC = item1.AtterC,
                AtterE  = item1.AtterE,
                AtterG  = item1.AtterG  
            });

但是,我收到关于加入的错误。

谁能帮帮我?

【问题讨论】:

  • 实际问题是什么?你能详细说明一下吗?
  • 尝试以下操作: ParentOne one = new ParentOne(); ParentTwo 二 = 新的 ParentTwo(); var results = (from o in one.AttrBList join t in two.AttrBList on o.id equals t.id select new { one = o, two = t}) .ToList();
  • 我在 parent1.AttrCList.Join 上收到错误消息“方法的类型参数 'EnumerableJoin(1Enumerable, lEnumerable, Func , Func, Func)' 无法从用法中推断出 Project.test01。请尝试显式指定类型参数。”

标签: c# linq join lambda


【解决方案1】:

是的,我解决了这个问题

 var res = from item1 in parent1.AttrCList
                  join item2 in AttrCList
                      on item1.id equals item2.id
                  select new
                  {
                         id= item1.id,
                         name = item2.name,
                         AtterC = item1.AtterC,
                         AtterE  = item1.AtterE,
                         AtterG  = item1.AtterG  
                  };

任何人,你知道,更快的解决方案?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-20
    • 1970-01-01
    • 1970-01-01
    • 2022-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-11
    相关资源
    最近更新 更多