C#将所有值替换为空字符串,但DataTable的DataColumn中除少数字符号外

时间:2015-11-09 08:45:51

标签: c# linq

我有DataTable,数据如下

Date         DateString
2015-01-01   1 Jan 2015
2015-01-02   2 Jan 2015
2015-01-03   3 Jan 2015
2015-01-04   4 Jan 2015
2015-01-05   5 Jan 2015
2015-01-06   6 Jan 2015
2015-01-07   7 Jan 2015
2015-01-08   8 Jan 2015
2015-01-09   9 Jan 2015
2015-01-10   10 Jan 2015
.            .
.            .

假设有n个记录且用户提供的值为3.那么它应该更新DateString列以仅返回3个值并将所有其他值清空,如下所示 -

Date         DateString
2015-01-01   1 Jan 2015
2015-01-02   
2015-01-03   
2015-01-04   
2015-01-05   5 Jan 2015
2015-01-06   
2015-01-07   
2015-01-08   
2015-01-09   
2015-01-10   10 Jan 2015
.            .
.            .

意思是,第一个值和最后一个值将始终保留,其他值可以使用Mean(或其他)保存。

我想使用LINQ。

2 个答案:

答案 0 :(得分:0)

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Date", typeof(DateTime));
            dt.Columns.Add("DateString", typeof(string));

            dt.Rows.Add(new object[] {DateTime.Parse("2015-01-01"), "1 Jan 2015"});
            dt.Rows.Add(new object[] {DateTime.Parse("2015-01-02"), "2 Jan 2015"});
            dt.Rows.Add(new object[] {DateTime.Parse("2015-01-03"), "3 Jan 2015"});
            dt.Rows.Add(new object[] {DateTime.Parse("2015-01-04"), "4 Jan 2015"});
            dt.Rows.Add(new object[] {DateTime.Parse("2015-01-05"), "5 Jan 2015"});
            dt.Rows.Add(new object[] {DateTime.Parse("2015-01-06"), "6 Jan 2015"});
            dt.Rows.Add(new object[] {DateTime.Parse("2015-01-07"), "7 Jan 2015"});
            dt.Rows.Add(new object[] {DateTime.Parse("2015-01-08"), "8 Jan 2015"});
            dt.Rows.Add(new object[] {DateTime.Parse("2015-01-09"), "9 Jan 2015"});
            dt.Rows.Add(new object[] { DateTime.Parse("2015-01-10"), "10 Jan 2015" });

            List<DateTime> findDates = new List<DateTime>() { DateTime.Parse("2015-01-01"), DateTime.Parse("2015-05-01"), DateTime.Parse("2015-10-01") };

            List<DataRow> unmatchRows = dt.AsEnumerable()
                .Where(x => !findDates.Contains(x.Field<DateTime>("Date"))).ToList();
            foreach (DataRow row in unmatchRows)
            {
                row["DateString"] = "";
            }


        }
    }
}
​

答案 1 :(得分:0)

这样的事情怎么样:

void RemoveDates(DataTable dataTable, int countToKeep)
{ 
    var indexesToKeep = IndexesToKeep(dataTable.Rows.Count, countToKeep);
    for (var i = 0; i < dataTable.Rows.Count; ++i)
    {
        if (!indexesToKeep.Contains(i))
        {
            dataTable.Rows[i]["DateString"] = "";
        }
    }
}

HashSet<int> IndexesToKeep(int totalCount, int countToKeep)
{
    var keep = new HashSet<int>();
    for (var i = 0; i < totalCount - 1 && keep.Count < countToKeep; i += totalCount / (countToKeep - 1))
    {
        keep.Add(i);
    }
    keep.Add(totalCount - 1);
    return keep;
}