删除值小于其他行的数据行
Remove Data Row with value less than other row
我有一个数据 Table,其中多行具有相同的“IPC”值但不同的“安全”值。
对于每个“IPC”,我对具有最高市场价值的行感兴趣并删除其他行。
举个例子:enter image description here
我如何在 C# 中通过简单的查询执行此操作?谢谢。
尝试以下操作:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication11
{
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("IPC", typeof(long));
dt.Columns.Add("Issuer", typeof(string));
dt.Columns.Add("Sector", typeof(string));
dt.Columns.Add("Mkt Value", typeof(long));
dt.Columns.Add("Security", typeof(string));
dt.Rows.Add(new object[] { 112345, "Ipsen", "Senior", 10910505, "AA" });
dt.Rows.Add(new object[] { 112345, "Ipsen", "Senior", 738427911, "AAA" });
dt.Rows.Add(new object[] { 112345, "Ipsen", "Senior", 51467951, "A" });
dt.Rows.Add(new object[] { 54321, "Legrand", "Hybrid", 485017008, "BBB" });
dt.Rows.Add(new object[] { 54321, "Legrand", "Hybrid", 28949439, "B+" });
dt.Rows.Add(new object[] { 54321, "Legrand", "Hybrid", 1299024462, "BB" });
var maxIPC = dt.AsEnumerable()
.OrderByDescending(x => x.Field<long>("Mkt Value"))
.GroupBy(x => x.Field<long>("IPC"))
.Select(x => new { row = x.FirstOrDefault(), sum = x.Sum(y => y.Field<long>("Mkt Value")) });
foreach (var row in maxIPC)
{
row.row["Mkt Value"] = row.sum;
}
DataTable maxIPCtable = maxIPC.Select(x => x.row).CopyToDataTable();
}
}
}
我有一个数据 Table,其中多行具有相同的“IPC”值但不同的“安全”值。
对于每个“IPC”,我对具有最高市场价值的行感兴趣并删除其他行。
举个例子:enter image description here
我如何在 C# 中通过简单的查询执行此操作?谢谢。
尝试以下操作:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication11
{
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("IPC", typeof(long));
dt.Columns.Add("Issuer", typeof(string));
dt.Columns.Add("Sector", typeof(string));
dt.Columns.Add("Mkt Value", typeof(long));
dt.Columns.Add("Security", typeof(string));
dt.Rows.Add(new object[] { 112345, "Ipsen", "Senior", 10910505, "AA" });
dt.Rows.Add(new object[] { 112345, "Ipsen", "Senior", 738427911, "AAA" });
dt.Rows.Add(new object[] { 112345, "Ipsen", "Senior", 51467951, "A" });
dt.Rows.Add(new object[] { 54321, "Legrand", "Hybrid", 485017008, "BBB" });
dt.Rows.Add(new object[] { 54321, "Legrand", "Hybrid", 28949439, "B+" });
dt.Rows.Add(new object[] { 54321, "Legrand", "Hybrid", 1299024462, "BB" });
var maxIPC = dt.AsEnumerable()
.OrderByDescending(x => x.Field<long>("Mkt Value"))
.GroupBy(x => x.Field<long>("IPC"))
.Select(x => new { row = x.FirstOrDefault(), sum = x.Sum(y => y.Field<long>("Mkt Value")) });
foreach (var row in maxIPC)
{
row.row["Mkt Value"] = row.sum;
}
DataTable maxIPCtable = maxIPC.Select(x => x.row).CopyToDataTable();
}
}
}