开发者

How Can We Get Date In Array From Dataset in C# Web Application?

开发者 https://www.devze.com 2023-03-17 17:03 出处:网络
how Would i get data in a new array from dataset i have four column in dataset but want to put only \"power\" and \"date\" co开发者_StackOverflow中文版lumn data in array ?

how Would i get data in a new array from dataset i have four column in dataset but want to put only "power" and "date" co开发者_StackOverflow中文版lumn data in array ?

Hopes for your suggestions..

Regards,


You could define a custom class which will represent your data:

public class Item
{
    public int Power { get; set; }
    public DateTime Date { get; set; }
}

and then you could use a LINQ query on your DataSet to extract the necessary information:

DataSet dataSet = ...
Item[] result = dataSet
    .Tables[0]
    .Rows
    .Cast<DataRow>()
    .Select(row => new Item
    {
        Power = row.Field<int>("power"),
        Date = row.Field<DateTime>("date"),
    })
    .ToArray();


Depending on your .NET version and final usage you could use LINQ to DataSet to pull it out and project to a new type.

Something like this would work, if working on a Typed Dataset

var limitedData = from x in myDataSet
                  select new { Power = x.power, MyDate = x.date };

Then you have an IEnumerable that you can work with, you could use .ToArray() if you really need it in an array.

0

精彩评论

暂无评论...
验证码 换一张
取 消