Заполнение DataGrid.
Код:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Data;
using OpcHda2Tcp.Common;
using OpcHda2Tcp.Client;
using System.Threading;
using System.Linq.Expressions;
namespace TestHDA
{
public static class PivotClass
{
public static DataTable ToPivotTable<T, TColumn, TRow, TData>(this IEnumerable<T> source, Func<T, TColumn> columnSelector, Expression<Func<T, TRow>> rowSelector, Func<IEnumerable<T>, TData> dataSelector)
{
DataTable table = new DataTable();
var rowName = ((MemberExpression)rowSelector.Body).Member.Name;
table.Columns.Add(new DataColumn(rowName));
var columns = source.Select(columnSelector).Distinct();
foreach (var column in columns)
table.Columns.Add(new DataColumn(column.ToString()));
var rows = source.GroupBy(rowSelector.Compile())
.Select(rowGroup => new
{
Key = rowGroup.Key,
Values = columns.GroupJoin(
rowGroup,
c => c,
r => columnSelector(r),
(c, columnGroup) => dataSelector(columnGroup))
});
foreach (var row in rows)
{
var dataRow = table.NewRow();
var items = row.Values.Cast<object>().ToList();
items.Insert(0, row.Key);
dataRow.ItemArray = items.ToArray();
table.Rows.Add(dataRow);
}
return table;
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
{
}
Opc2TCPClient myClient = new Opc2TCPClient("127.0.0.1", 3000, 64 * 1024);
bool isConnected = myClient.Connect();
if (isConnected)
{
string message = @"2017-03-28 20:56:00%2017-03-28 20:57:00%5%Random.Int1,Random.Int2,Random.Int4";
byte[] data = Util.MakeMessage(message);
myClient.SyncSend(data);
myClient.AsyncRead();
while (true)
{
if (myClient.RecivedAll)
{
string msg = "";
bool b = Util.VeryfyMessage(myClient.RecivedData.ToArray(), out msg);
if (!b)
{
}
else
{
DataTable dt = Util.JsonToDataTable(msg);
DataTable myDataTable = dt;
var data2 = myDataTable.AsEnumerable().Select(x => new { Timestamp = x.Field<string>("Timestamp"), TagName = x.Field<string>("TagName"), Value = x.Field<string>("Value") });
DataTable dt1 = data2.ToPivotTable(item => item.TagName, item => item.Timestamp, items => items.Any() ? items.Sum(x => double.Parse(x.Value)) : 0);
MyDataGrid.ItemsSource = dt1.DefaultView;
}
break;
}
Thread.Sleep(100);
}
}
myClient.Close();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Data;
using OpcHda2Tcp.Common;
using OpcHda2Tcp.Client;
using System.Threading;
using System.Linq.Expressions;
namespace TestHDA
{
public static class PivotClass
{
public static DataTable ToPivotTable<T, TColumn, TRow, TData>(this IEnumerable<T> source, Func<T, TColumn> columnSelector, Expression<Func<T, TRow>> rowSelector, Func<IEnumerable<T>, TData> dataSelector)
{
DataTable table = new DataTable();
var rowName = ((MemberExpression)rowSelector.Body).Member.Name;
table.Columns.Add(new DataColumn(rowName));
var columns = source.Select(columnSelector).Distinct();
foreach (var column in columns)
table.Columns.Add(new DataColumn(column.ToString()));
var rows = source.GroupBy(rowSelector.Compile())
.Select(rowGroup => new
{
Key = rowGroup.Key,
Values = columns.GroupJoin(
rowGroup,
c => c,
r => columnSelector(r),
(c, columnGroup) => dataSelector(columnGroup))
});
foreach (var row in rows)
{
var dataRow = table.NewRow();
var items = row.Values.Cast<object>().ToList();
items.Insert(0, row.Key);
dataRow.ItemArray = items.ToArray();
table.Rows.Add(dataRow);
}
return table;
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
{
}
Opc2TCPClient myClient = new Opc2TCPClient("127.0.0.1", 3000, 64 * 1024);
bool isConnected = myClient.Connect();
if (isConnected)
{
string message = @"2017-03-28 20:56:00%2017-03-28 20:57:00%5%Random.Int1,Random.Int2,Random.Int4";
byte[] data = Util.MakeMessage(message);
myClient.SyncSend(data);
myClient.AsyncRead();
while (true)
{
if (myClient.RecivedAll)
{
string msg = "";
bool b = Util.VeryfyMessage(myClient.RecivedData.ToArray(), out msg);
if (!b)
{
}
else
{
DataTable dt = Util.JsonToDataTable(msg);
DataTable myDataTable = dt;
var data2 = myDataTable.AsEnumerable().Select(x => new { Timestamp = x.Field<string>("Timestamp"), TagName = x.Field<string>("TagName"), Value = x.Field<string>("Value") });
DataTable dt1 = data2.ToPivotTable(item => item.TagName, item => item.Timestamp, items => items.Any() ? items.Sum(x => double.Parse(x.Value)) : 0);
MyDataGrid.ItemsSource = dt1.DefaultView;
}
break;
}
Thread.Sleep(100);
}
}
myClient.Close();
}
}
}
что вам отладчик говорит? пройдитесь пошагово, сразу найдете ошибку.