Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ使用C将数列交换为数据表的行_C#_Linq_.net 3.5_.net 4.5 - Fatal编程技术网

C# LINQ使用C将数列交换为数据表的行

C# LINQ使用C将数列交换为数据表的行,c#,linq,.net-3.5,.net-4.5,C#,Linq,.net 3.5,.net 4.5,我有一个数据表: location Quarter ppl_required ppl_available BLR Q1 70 35 BLR Q2 50 45 BLR Q3 25 28 BLR Q4 60 58 CHN Q1 77

我有一个数据表:

location    Quarter   ppl_required   ppl_available
BLR          Q1        70             35
BLR          Q2        50             45
BLR          Q3        25             28
BLR          Q4        60             58
CHN          Q1        77             92
CHN          Q2        42             66
CHN          Q3        29             20
CHN          Q4        22             24
var newSet = dt.AsEnumerable()
               .GroupBy(r => r.Field<string>("Location"))
               .Select(g => new
               {
                    Location = g.Key,
                    ppl_required_Q1 = g.Where(p => p.Field<string>("Quarter") == "Q1").Sum(p => p.Field<int>("ppl_required")),
                    ppl_required_Q2 = g.Where(p => p.Field<string>("Quarter") == "Q2").Sum(p => p.Field<int>("ppl_required")),
                    ppl_required_Q3 = g.Where(p => p.Field<string>("Quarter") == "Q3").Sum(p => p.Field<int>("ppl_required")),
                    ppl_required_Q4 = g.Where(p => p.Field<string>("Quarter") == "Q4").Sum(p => p.Field<int>("ppl_required")),
                    ppl_available_Q1 = g.Where(p => p.Field<string>("Quarter") == "Q1").Sum(p => p.Field<int>("ppl_available")),
                    ppl_available_Q2 = g.Where(p => p.Field<string>("Quarter") == "Q2").Sum(p => p.Field<int>("ppl_available")),
                    ppl_available_Q3 = g.Where(p => p.Field<string>("Quarter") == "Q3").Sum(p => p.Field<int>("ppl_available")),
                    ppl_available_Q4 = g.Where(p => p.Field<string>("Quarter") == "Q4").Sum(p => p.Field<int>("ppl_available")),
                });
是否有更好的方法可以使用LINQ或使用.NET3.5/4.0/4.5 framework的高级LINQ以非常简单或短的方式[无循环]将下面的数据表作为输出

Location  ppl_Required_Q1  ppl_Required_Q2  ppl_Required_Q3  ppl_Required_Q4  ppl_available_Q1  ppl_available_Q2  ppl_available_Q3  ppl_available_Q4
BLR       70               50               25               60               35                45                28                58
CHN       77               42               29               22               92                66                20                24

我不确定您尝试了什么,是否有任何效率或灵活性需求,或者您对输出容器真正需要什么,但也许像这样简单的东西是有用的。假设dt是您的数据表:

location    Quarter   ppl_required   ppl_available
BLR          Q1        70             35
BLR          Q2        50             45
BLR          Q3        25             28
BLR          Q4        60             58
CHN          Q1        77             92
CHN          Q2        42             66
CHN          Q3        29             20
CHN          Q4        22             24
var newSet = dt.AsEnumerable()
               .GroupBy(r => r.Field<string>("Location"))
               .Select(g => new
               {
                    Location = g.Key,
                    ppl_required_Q1 = g.Where(p => p.Field<string>("Quarter") == "Q1").Sum(p => p.Field<int>("ppl_required")),
                    ppl_required_Q2 = g.Where(p => p.Field<string>("Quarter") == "Q2").Sum(p => p.Field<int>("ppl_required")),
                    ppl_required_Q3 = g.Where(p => p.Field<string>("Quarter") == "Q3").Sum(p => p.Field<int>("ppl_required")),
                    ppl_required_Q4 = g.Where(p => p.Field<string>("Quarter") == "Q4").Sum(p => p.Field<int>("ppl_required")),
                    ppl_available_Q1 = g.Where(p => p.Field<string>("Quarter") == "Q1").Sum(p => p.Field<int>("ppl_available")),
                    ppl_available_Q2 = g.Where(p => p.Field<string>("Quarter") == "Q2").Sum(p => p.Field<int>("ppl_available")),
                    ppl_available_Q3 = g.Where(p => p.Field<string>("Quarter") == "Q3").Sum(p => p.Field<int>("ppl_available")),
                    ppl_available_Q4 = g.Where(p => p.Field<string>("Quarter") == "Q4").Sum(p => p.Field<int>("ppl_available")),
                });
编辑

添加一个由示例组装而成的扩展方法,以防将来链接断开。您应该能够根据需要对此进行修改

public static DataTable ToDataTable<T>(this IEnumerable<T> source, string newTableName)
{
    DataTable newTable = new DataTable(newTableName);

    T firstRow = source.FirstOrDefault();
    if (firstRow != null)
    {
        PropertyInfo[] properties = firstRow.GetType().GetProperties();
        foreach (PropertyInfo prop in properties)
        {
            newTable.Columns.Add(prop.Name, prop.PropertyType);
        }

        foreach (T element in source)
        {
            DataRow newRow = newTable.NewRow();
            foreach (PropertyInfo prop in properties)
            {
                newRow[prop.Name] = prop.GetValue(element, null);
            }
            newTable.Rows.Add(newRow);
        }
    }
    return newTable;
}

我创建了一个与您在LINQPad中描述的类似的数据结构。这是我的代码

void Main()
{
    List<Location> locations = new List<Location>
   {
      new Location { Key = "BLR", Quarter = "Q1", PeopleRequired = 70, PeopleAvailable = 35 },
      new Location { Key = "BLR", Quarter = "Q2", PeopleRequired = 50, PeopleAvailable = 45 },
      new Location { Key = "BLR", Quarter = "Q3", PeopleRequired = 25, PeopleAvailable = 28 },
      new Location { Key = "BLR", Quarter = "Q4", PeopleRequired = 60, PeopleAvailable = 58 },
      new Location { Key = "CHN", Quarter = "Q1", PeopleRequired = 77, PeopleAvailable = 92 },
      new Location { Key = "CHN", Quarter = "Q2", PeopleRequired = 42, PeopleAvailable = 66 },
      new Location { Key = "CHN", Quarter = "Q3", PeopleRequired = 29, PeopleAvailable = 20 },
      new Location { Key = "CHN", Quarter = "Q4", PeopleRequired = 22, PeopleAvailable = 24 },
      new Location { Key = "CAD", Quarter = "Q1", PeopleRequired = 100, PeopleAvailable = 150 },
      new Location { Key = "CAD", Quarter = "Q2", PeopleRequired = 200, PeopleAvailable = 250 },
   };

   var results =
   (
      from loc in locations.Select(l => new { l.Key }).Distinct()
      join q1 in locations.Where(l => l.Quarter == "Q1") on loc.Key equals q1.Key into quarter1
      join q2 in locations.Where(l => l.Quarter == "Q2") on loc.Key equals q2.Key into quarter2
      join q3 in locations.Where(l => l.Quarter == "Q3") on loc.Key equals q3.Key into quarter3
      join q4 in locations.Where(l => l.Quarter == "Q4") on loc.Key equals q4.Key into quarter4
      from q1 in quarter1.DefaultIfEmpty()
      from q2 in quarter2.DefaultIfEmpty()
      from q3 in quarter3.DefaultIfEmpty()
      from q4 in quarter4.DefaultIfEmpty()
      select new
      {
         loc.Key,
         Q1_PeopleRequired  = q1 != null ? q1.PeopleRequired  : -1,
         Q1_PeopleAvailable = q1 != null ? q1.PeopleAvailable : -1,
         Q2_PeopleRequired  = q2 != null ? q2.PeopleRequired  : -1,
         Q2_PeopleAvailable = q2 != null ? q2.PeopleAvailable : -1,
         Q3_PeopleRequired  = q3 != null ? q3.PeopleRequired  : -1,
         Q3_PeopleAvailable = q3 != null ? q3.PeopleAvailable : -1,
         Q4_PeopleRequired  = q4 != null ? q4.PeopleRequired  : -1,
         Q4_PeopleAvailable = q4 != null ? q4.PeopleAvailable : -1
      }
   );

   results.Dump();
}

// Define other methods and classes here
public class Location
{
   public string Key          { get; set; }
   public string Quarter      { get; set; }
   public int PeopleRequired  { get; set; }
   public int PeopleAvailable { get; set; }
}

为什么有更好的方法?你已经做到了。严肃的:比什么好?显示您首先尝试的内容。如何将新闻集数据的结果分配/存储到新的数据表??是否有方法直接将结果新闻集绑定到另一个新的数据表??您应该能够为此使用扩展方法。考虑ToDATABLE示例和。