I am pulling some historical data from Firebird database as below:
Product_ID Date Price
1 2001-01-01 10
1 2001-02-01 10
1 2001-03-01 15
1 2001-04-01 10
1 2001-05-01 20
1 2001-06-01 20
What I am trying to do is to extract the first for occurrence every price change.
Example of expected data set:
Product_ID Date Price
1 2001-01-01 10
1 2001-03-01 15
1 2001-04-01 10
1 2001-05-01 20
I know that on MSSQL I could leverage LAG for that. Is it possible to do that with Firebird?
You can try this, but be aware I didn't tested it:
CREATE PROCEDURE SP_Test
RETURNS (
Product_ID INTEGER,
Date DATE,
Price INTEGER
)
AS
DECLARE VARIABLE Last_Product_ID INTEGER;
DECLARE VARIABLE Last_Date DATE;
DECLARE VARIABLE Last_Price INTEGER;
BEGIN
FOR SELECT Product_ID, Date, Price
FROM xxxx
ORDER BY Product_ID, Date
INTO Product_ID, Date, Price
DO BEGIN
IF ((:Last_Product_ID IS NULL) OR
(:Last_Date IS NULL) OR
(:Last_Price IS NULL) OR
(:Product_ID <> :Last_Product_ID) OR
(:Price <> :Last_Price)) THEN
SUSPEND;
Last_Product_ID = :Product_ID;
Last_Date = :Date;
Last_Price = :Price;
END;
END;
in MoreLinq there is a Lag extension method but it is supported only in Linq to Objects...
What you can do, if you are looking for a C# linq answer for that you can:
Basically order your data the correct way and then add a row index for while price (and product_id) is still the same. Then group by it and select the min date.
int groupingIndex = 0;
int previousPrice = 0;
var response = data
.OrderBy(item => item.Product_ID)
.ThenBy(item => item.Date)
.Select(item =>
{
if (item.Price != previousPrice)
{
previousPrice = item.Price;
groupingIndex++;
}
return new { Index = groupingIndex, Item = item };
})
.GroupBy(item => new { item.Index, item.Item.Product_ID, item.Item.Price } )
.Select(group => new Record
{
Product_ID = group.Key.Product_ID,
Price = group.Key.Price,
Date = group.Min(item => item.Item.Date)
}).ToList();
And if you don't mind doing the operation in the C# and not the DB (and using a beta version of the MoreLinq) then:
int index = 0;
var result2 = data
.OrderBy(item => item.Product_ID)
.ThenBy(item => item.Date)
.Lag(1, (current, previous) => new { Index = (current.Price == previous?.Price ? index : ++index), Item = current })
.GroupBy(item => new { item.Index, item.Item.Product_ID, item.Item.Price })
.Select(group => new Record { Product_ID = group.Key.Product_ID, Price = group.Key.Price, Date = group.Min(item => item.Item.Date) })
.ToList();
This is a little complicated but it works
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("Product_ID", typeof(int));
dt.Columns.Add("Date", typeof(DateTime));
dt.Columns.Add("Price", typeof(int));
dt.Rows.Add(new object[] {1, DateTime.Parse("2001-01-01"), 10});
dt.Rows.Add(new object[] {1, DateTime.Parse("2001-02-01"), 10});
dt.Rows.Add(new object[] {1, DateTime.Parse("2001-03-01"), 15});
dt.Rows.Add(new object[] {1, DateTime.Parse("2001-04-01"), 10});
dt.Rows.Add(new object[] {1, DateTime.Parse("2001-05-01"), 20});
dt.Rows.Add(new object[] {1, DateTime.Parse("2001-06-01"), 20});
dt.Rows.Add(new object[] { 2, DateTime.Parse("2001-01-01"), 10 });
dt.Rows.Add(new object[] { 2, DateTime.Parse("2001-02-01"), 10 });
dt.Rows.Add(new object[] { 2, DateTime.Parse("2001-03-01"), 15 });
dt.Rows.Add(new object[] { 2, DateTime.Parse("2001-04-01"), 10 });
dt.Rows.Add(new object[] { 2, DateTime.Parse("2001-05-01"), 20 });
dt.Rows.Add(new object[] { 2, DateTime.Parse("2001-06-01"), 20 });
dt = dt.AsEnumerable().OrderBy(x => x.Field<DateTime>("Date")).CopyToDataTable();
List<DataRow> results = dt.AsEnumerable()
.GroupBy(g => g.Field<int>("Product_ID"))
.Select(g1 => g1.Select((x, i) => new { row = x, dup = (i == 0) || ((i > 0) && (g1.Skip(i - 1).FirstOrDefault().Field<int>("Price") != g1.Skip(i).FirstOrDefault().Field<int>("Price"))) ? false : true })
.Where(y => y.dup == false).Select(z => z.row)).SelectMany(m => m).ToList();
}
}
}
Related
I have got this situation with a datatable like this
C1 C2 C3
A AA 4
BB 6
B CC 3
DD 3
EE 4
C FF 5
GG 5
and my output should be like this
C1 C2 C3
A AA,BB 10
B CC,DD,EE 10
C FF,GG 10
How can i group by the column with the space till the next value comes up
What i did was i took all the row itemarray and then using some string manipulation and regex got the row value as for the first two values like this and assigned to a variable in a query using Let
A,AA,BB,10|B,CC,DD,EE,10 but then i cannot add it using the
**DT.clone.rows.Add(x.split("|"c))* method as there its not incrementing and adding the whole joined string
Any other input where i can manipulate and add it (P.S i know linq is querying language)
Thank you for your time
You can use .GroupBy to get result needed
Here is your class:
public class Data
{
public string C1 { get; set; }
public string C2 { get; set; }
public int C3 { get; set; }
}
Imagine that you have list of Data objects, so your GroupBy expression will be following:
var result = list.GroupBy(g => g.C1, (a, b) => new {C1 = a, C2 = b.ToList()})
.Select(g => new
{
g.C1,
C2 = string.Join(",", g.C2.Select(m => m.C2)),
C3 = g.C2.Sum(m => m.C3)
})
.ToList();
A simple .GroupBy can give you expected result, Edited to handle Null or WhiteSpace Columns
var res = ListModel.Where(e => !string.IsNullOrWhiteSpace(e.C1)
&& !string.IsNullOrWhiteSpace(e.C2))
.GroupBy(e => e.C1).Select(e => new
{
e.Key,
c2 = string.Join(",", e.Select(x => x.C2).ToList()),
c3 = e.Sum(x => x.C3)
}).ToList();
Hello All first of all Thank you for your time and effort i Did this use case using this code
This gave me all row item array in string and than in the end with a little Split method i was able to add it to my datatable
String.Join("|",(System.Text.RegularExpressions.Regex.Replace(String.Join("|",(From roww In DT.AsEnumerable() Select String.Join(",",roww.ItemArray) ).ToList),"\|,",",")).Split("|"c).
Select(Function(q)CStr(q)+","+CStr(String.join("|",System.Text.RegularExpressions.Regex.Matches(CStr(q),"\d+").Cast(Of match)).Split("|"c).Sum(Function(r) CInt(r) ))).tolist),",\d+,",",")```
Try following code which is tested
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 dt1 = new DataTable();
dt1.Columns.Add("C1", typeof(string));
dt1.Columns.Add("C2", typeof(string));
dt1.Columns.Add("C3", typeof(int));
dt1.Rows.Add(new object[] { "A", "AA", 4});
dt1.Rows.Add(new object[] { null, "BB", 6});
dt1.Rows.Add(new object[] { "B", "CC", 3});
dt1.Rows.Add(new object[] { null, "DD", 3});
dt1.Rows.Add(new object[] { null, "EE", 4});
dt1.Rows.Add(new object[] { "C", "FF", 5});
dt1.Rows.Add(new object[] { null, "GG", 5});
//replace nulls in column 1 with actual values
string previous = "";
foreach(DataRow row in dt1.AsEnumerable())
{
if (row.Field<string>("C1") == null)
{
row["C1"] = previous;
}
else
{
previous = row.Field<string>("C1");
}
}
DataTable dt2 = dt1.Clone();
var groups = dt1.AsEnumerable().GroupBy(x => x.Field<string>("C1")).ToList();
foreach (var group in groups)
{
dt2.Rows.Add(new object[] {
group.Key,
string.Join(",", group.Select(x => x.Field<string>("C2"))),
group.Select(x => x.Field<int>("C3")).Sum()
});
}
}
}
}
Yet another way using Skip, TakeWhile, and GroupBy extensions:
DataTable dt1 = new DataTable();
dt1.Columns.Add("C1", typeof(string));
dt1.Columns.Add("C2", typeof(string));
dt1.Columns.Add("C3", typeof(int));
//The output table.
DataTable dt2 = dt1.Clone();
dt1.Rows.Add(new object[] { "A", "AA", 3 });
dt1.Rows.Add(new object[] { null, "BB", 6 });
dt1.Rows.Add(new object[] { "B", "CC", 3 });
dt1.Rows.Add(new object[] { null, "DD", 3 });
dt1.Rows.Add(new object[] { null, "EE", 4 });
dt1.Rows.Add(new object[] { "C", "FF", 5 });
dt1.Rows.Add(new object[] { null, "GG", 6 });
var rows = dt1.Rows.Cast<DataRow>().AsEnumerable();
foreach (var row in rows.Where(r => r.Field<string>("C1") != null))
{
var indx = dt1.Rows.IndexOf(row) + 1;
var q = rows
.Skip(indx)
.TakeWhile(t => t.Field<string>("C1") == null)
.GroupBy(g => g.Field<string>("C1"))
.Select(g => new
{
C1 = row.Field<string>("C1"),
C2 = $"{row.Field<string>("C2")}, {string.Join(", ", g.Select(s => s.Field<string>("C2")))}",
C3 = row.Field<int>("C3") + g.Sum(s => s.Field<int>("C3")),
}).FirstOrDefault();
if (q != null)
dt2.Rows.Add(q.C1, q.C2, q.C3);
}
dataGridView1.DataSource = null;
dataGridView1.DataSource = dt2;
The idea behind this snippet is to:
Get the complete rows and iterate through them.
For each complete row, we get it's index from the original DataTable and add 1 to make a starting search point for the incomplete rows. The Skip extension is the method to achieve that.
The TakeWhile extension function gets the incomplete rows and stops at the next complete row.
The GroupBy extension function groups the incomplete rows to concatenate their C2 values and sum their C3 values, add the results to the values of the complete row and create a temporary anonymous object to hold these values.
Extract the anonymous object and add a new DataRow to the output DataTable.
And finally, bind the output DataTable to a DGV.
Happy 2020 for all.
I would like to retrieve a list of records the given Year, class for the latest effective date.
grouping by Class, Year, RangeMin, RangeMax
Id Class...Year...EffectiveDate...Value...RangeMin...RangeMax
1. A.......2019....2019/1/1.........850......1.........100
2. A.......2019....2019/1/15........840......1.........100
3. A.......2019....2019/2/1.........550......101.......200
4. B.......2019....2019/1/5.........540......1.........100
5. B.......2020....2019/1/5.........650......1.........100
6. B.......2020....2019/5/1.........670......101.......200
7. B.......2020....2019/5/2.........680......101.......200
So if I'm querying for all records which are class A and year 2019 to return a list of rows: 2,3
If I'm querying for all records which are class B and year 2020 to return a list of rows: 5,7
var recordsInDb = (from record in context.records where record.Year == year & record.Class == class_ select record).ToList();
So far I have been able to get a list of all the records for given year, class.
I know I could add a order by descending on the effective date. Yet that still returns all the records not just the ones which have the highest effective date.
You can just group by your criteria and then select the latest from each group.
Using query syntax:
var recordsInDb = (from record in context.records
where record.Year == year & record.Class == class_
group record by new { record.Year, record.Class, record.RangeMin, record.RangeMax } into rg
select (
from record in rg
orderby record.EffectiveDate
select record
).Last()
)
.ToList();
I think it is a little easier to follow using Fluent/lambda syntax since the Last requires it anyway:
var ans = context.records.Where(r => r.Year == year && r.Class == class_)
.GroupBy(r => new { r.Year, r.Class, r.RangeMin, r.RangeMax })
.Select(rg => rg.OrderBy(r => r.EffectiveDate).Last())
.ToList();
Try following :
DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Class", typeof(string));
dt.Columns.Add("Year", typeof(int));
dt.Columns.Add("EffectiveDate", typeof(DateTime));
dt.Columns.Add("Value", typeof(int));
dt.Columns.Add("RangeMin", typeof(int));
dt.Columns.Add("RangeMax", typeof(int));
dt.Rows.Add(new object[] {1, "A", 2019, DateTime.Parse("2019/1/1"), 850,1, 100});
dt.Rows.Add(new object[] {2, "A", 2019, DateTime.Parse("2019/1/15"), 840,1, 100});
dt.Rows.Add(new object[] {3, "A", 2019, DateTime.Parse("2019/2/1"), 550,101, 200});
dt.Rows.Add(new object[] {4, "B", 2019, DateTime.Parse("2019/1/5"), 540,1, 100});
dt.Rows.Add(new object[] {5, "B", 2020, DateTime.Parse("2019/1/5"), 650,1, 100});
dt.Rows.Add(new object[] {6, "B", 2020, DateTime.Parse("2019/5/1"), 670,101, 200});
dt.Rows.Add(new object[] {7, "B", 2020, DateTime.Parse("2019/5/2"), 680,101, 200});
DataTable results = dt.AsEnumerable()
.OrderByDescending(x => x.Field<int>("Year"))
.ThenByDescending(x => x.Field<DateTime>("EffectiveDate"))
.GroupBy(x => new { cl = x.Field<string>("Class"), month = new DateTime(x.Field<DateTime>("EffectiveDate").Year, x.Field<DateTime>("EffectiveDate").Month, 1) })
.Select(x => x.FirstOrDefault())
.CopyToDataTable();
I have Data Table with the following data
Number Type Order count
1 1 R 1
1 1 R 1
1 1 R 1
1 2 R 1
I am looking to get to this result
Number Type Order count
1 1 R 3
1 2 R 1
How can I group by three columns
var result = dt.AsEnumerable()
.GroupBy(x => {x.Field<string>("Number"))//need to group by Type and order also need to sum te total counts
rgoal
Your question made me curious, so I did some digging on Stack Overflow.
esc's answer appears will also solve your issue. It is posted under: How do I use SELECT GROUP BY in DataTable.Select(Expression)?:
Applying his method to your problem gave me this solution:
DataTable dt2 = dt.AsEnumerable()
.GroupBy(r => new { Number = r["Number"], Type = r["Type"], Order = r["Order"] })
.Select(g =>
{
var row = dt.NewRow();
row["Number"] = g.Key.Number;
row["Type"] = g.Key.Type;
row["Order"] = g.Key.Order;
row["Count"] = g.Count();
return row;
}).CopyToDataTable();
This will return a DataTable matching the schema of the input DataTable with the grouping and counts you requested.
Here is the full code I use to verify in LINQPad:
DataTable dt = new DataTable("Demo");
dt.Columns.AddRange
(
new DataColumn[]
{
new DataColumn ( "Number", typeof ( int ) ),
new DataColumn ( "Type", typeof ( int ) ),
new DataColumn ( "Order", typeof ( string ) ),
new DataColumn ( "Count", typeof ( int ) )
}
);
dt.Rows.Add(new object[] { 1,1,"R", 1 });
dt.Rows.Add(new object[] { 1,1,"R", 1 });
dt.Rows.Add(new object[] { 1,1,"R", 1 });
dt.Rows.Add(new object[] { 1,2,"R", 1 });
DataTable dt2 = dt.AsEnumerable()
.GroupBy(r => new { Number = r["Number"], Type = r["Type"], Order = r["Order"] })
.Select(g =>
{
var row = dt.NewRow();
row["Number"] = g.Key.Number;
row["Type"] = g.Key.Type;
row["Order"] = g.Key.Order;
row["Count"] = g.Count();
return row;
}).CopyToDataTable();
foreach (DataRow row in dt2.Rows)
{
for (int i = 0; i < dt2.Columns.Count; i++)
Console.Write("{0}{1}",
row[i], // Print column data
(i < dt2.Columns.Count - 1)? " " : Environment.NewLine); // Print column or row separator
}
Here are the results:
1 1 R 3
1 2 R 1
I have a collection of Orders which are pulled from EF. Each Order has an order date:
public class Order {
public DateTime Date { get; set; }
public int Id { get; set; }
}
I want to be able to run a query to return the number of orders for each day in a certain date range. The query method should look something like:
public class ICollection<OrderDateSummary> GetOrderTotalsForDateRange(DateTime startDate, DateTime endDate) {
var orderDateSummary = Set.SelectMany(u => u.Orders) // ..... grouping/totalling here?!
return orderDateSummary;
}
For info, Set is actually part of a repository which returns a User aggregate root, so the type of Set is DbSet<User> The bit I am stuck on is grouping and totalling the Orders queryable from the SelectMany method.
The OrderDateSummary class looks like:
public OrderDateSummary {
DateTime Date { get; set; }
int Total { get; set; }
}
So, the output for a start date of 01/01/2016 and an end date of 03/01/2016 would look something like:
Date Total
===================
01/01/2016 10
02/01/2016 2
03/01/2016 0
04/01/2016 12
As I can see you need to generate all dates in range from start to end. Then calculate total number of orders on each date.
DateTime start = new DateTime(2016, 1, 1);
DateTime end = new DateTime(2016, 1, 4);
Enumerable
.Range(0, 1 + (end - start).Days)
.Select(x => start.AddDays(x))
.GroupJoin(Set.SelectMany(u => u.Orders),
dt => dt, o => o.Date.Date,
(dt, orders) => new OrderDateSummary { Date = dt, Total = orders.Count() })
.ToList();
Check out working example on Ideone.
var startDate = new DateTime (2016, 1, 1);
var endDate = new DateTime (2016, 1, 4);
Set.SelectMany(u => u.Orders).
Where (order => startDate <= order.Date && order.Date <= endDate) // If filter needed
GroupBy (order => order.Date, (date, values) =>
new OrderDateSummary () {
Date = date,
Total = values.Count ()
}).
OrderBy (summary => summary.Date).
ToList ();
Just you should mark your OrderDateSummary with class or struct and make those properties public or add constructor.
And you have a date 04/01/2016 in expected result, so, I guess, your end time is 4th and not 3th.
Try code below which is linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication82
{
class Program
{
static void Main(string[] args)
{
List<OrderDateSummary> orderSummary = null;
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("date", typeof(DateTime));
dt.Columns.Add("amount", typeof(decimal));
dt.Rows.Add(new object[] { 1, DateTime.Parse("1/1/16"), 1.00 });
dt.Rows.Add(new object[] { 2, DateTime.Parse("1/1/16"), 2.00 });
dt.Rows.Add(new object[] { 3, DateTime.Parse("1/2/16"), 3.00 });
dt.Rows.Add(new object[] { 4, DateTime.Parse("1/2/16"), 4.00 });
dt.Rows.Add(new object[] { 5, DateTime.Parse("1/2/16"), 5.00 });
dt.Rows.Add(new object[] { 6, DateTime.Parse("1/3/16"), 6.00 });
dt.Rows.Add(new object[] { 7, DateTime.Parse("1/3/16"), 7.00 });
orderSummary = dt.AsEnumerable()
.GroupBy(x => x.Field<DateTime>("date"))
.Select(x => new OrderDateSummary() { Date = x.Key, Total = x.Count() })
.ToList();
}
}
public class OrderDateSummary {
public DateTime Date { get; set; }
public int Total { get; set; }
}
}
how about
List<OrderDateSummary> Result = OrderList
.Where(x => x.Date >= startDate && x.Date <= endDate)
.GroupBy(x => x.Date)
.Select(z => new OrderDateSummary(){
Date = z.Key,
Total = z.Count()
}).OrderBy(d=> d.Date).ToList();
I have a datagridview control on my windows form.
Now i need to select max and min value of a column.
In data-table we can do this by using this
Code
int maxID = curriculmDataTable.AsEnumerable().Max(r => r.Field<int>("Id"));
How can i achieve this in datagridview.
You can try:
var MaxID = dataGridView1.Rows.Cast<DataGridViewRow>()
.Max(r => Convert.ToInt32(r.Cells["Id"].Value));
Make sure your Id cell has int type value, otherwise use Int.TryParse like:
int temp;
var MaxID2 = dataGridView1.Rows.Cast<DataGridViewRow>()
.Max(r => int.TryParse(r.Cells["Id"].Value.ToString(), out temp) ?
temp : 0 );
If your datasource is a datatable this sample can help you.
DataTable dtDataSource = new DataTable();
dtDataSource.Columns.Add("Value");
dtDataSource.Columns.Add("Display");
dtDataSource.Rows.Add(new object[] { 1, 1});
dtDataSource.Rows.Add(new object[] { 2, 2 });
dtDataSource.Rows.Add(new object[] { 3, 3 });
dtDataSource.Rows.Add(new object[] { 4, 4 });
dtDataSource.Rows.Add(new object[] { 5, 5 });
var results = dtDataSource.AsEnumerable().Max(row => Convert.ToInt32(row["Value"]));
datatable dt=datagridview1.datasource as datatable
int total = dt.Compute("Sum ( ColumnName ) ", "Criteria");