【问题标题】:Convert this Linq code to regular/non linq code将此 Linq 代码转换为常规/非 linq 代码
【发布时间】:2014-04-04 07:50:30
【问题描述】:

我是 Linq 的新手,正在处理与我完全相反的某人的代码。他在下面写了这段代码。我理解代码,但谁能告诉我是否可以用几乎相同数量的代码将此代码转换为非 linq

public static AType MyFunction() 
{
          return new AType 
          {
              PropertyOfAType = SomeIEnumerable.Select(r => new BType
              {
                  Property1OfBType =  r.GetData(5),
                  Property2OfBType =  r.GetData(6)

              }).ToArray()
          };

}

【问题讨论】:

  • 只需使用Array.ConvertAll()
  • 抱歉,我看不到 Array.ConvertAll() 将如何达到预期的效果。我没有将一种类型的数组转换为另一种。你能写下代码吗?
  • Array.ConvertAll.Select() 相同,只是需要将输入转换为数组。

标签: linq-to-objects


【解决方案1】:
    public static AType MyFunction()
    {
        AType aType = new AType();
        List<BType> bTypes = new List<BType>();
        foreach (var r in SomeIEnumerable)
        {
            bTypes.Add(new BType
            {
                Property1OfBType = r.GetData(5),
                Property2OfBType = r.GetData(6)
            });
        }
        aType.PropertyOfAType = bTypes.ToArray();
        return aType;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-09
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多