Ich möchte aus der ersten Tabelle eine Liste mit Produkten beziehen. Diese können pro Land und Typ und Datum unterschiedliche Preise haben. Außerdem kann sich der Preis historisch ändern. In der zweiten Tabelle gibt es kein Enddatum für den Preis. Wie stelle ich es an wenn ich nun den Preis zu einem bestimmten Datum bestimmen möchte. Beispiel Tabelle 1. KundenNummer, Land, Produkt, Anzahl, KaufDatum 0815, Deutschland, A1, 66, 01.12.1999 0815, Deutschland, A1, 77, 01.03.2001 0815, Deutschland, A1, 88, 01.03.2005 0815, Deutschland, A1, 22, 01.03.2009
Das max Startdatum muss <= dem KaufDatum vom für das jeweilige Land und Produkt sein, da ich ja ansonsten für den 01.03.2001 den Preis vom 01.01.1999 und 01.01.2001 erhalten würde.
Wie bringe ich das nun in VB.NET mit Linq zusammen?
Dim table1 = New Table1() Dim table2 = New Table2()
table1.Add(New Table1.Item("0815", "Deutschland", "A1", 66, New DateTime(1999, 12, 1))) table1.Add(New Table1.Item("0815", "Deutschland", "A1", 77, New DateTime(2001, 3, 1))) table1.Add(New Table1.Item("0815", "Deutschland", "A1", 88, New DateTime(2005, 3, 1))) table1.Add(New Table1.Item("0815", "Deutschland", "A1", 22, New DateTime(2009, 3, 1)))
table2.Add(New Table2.Item("Deutschland", 10, "A1", New DateTime(1999, 1, 1))) table2.Add(New Table2.Item("Deutschland", 15, "A1", New DateTime(2001, 1, 1))) table2.Add(New Table2.Item("Deutschland", 25, "A1", New DateTime(2009, 1, 1)))
Dim table3 = _ From t1 In table1 _ Let t2 = (From item In Table2 _ Where item.Country = t1.Country _ AndAlso item.Product = t1.Product _ AndAlso item.StartDate <= t1.PurchaseDate _ Order By item.StartDate Descending).FirstOrDefault() Select New With { _ .Customer = t1.Customer, _ .Country = t1.Country, _ .Product = t1.Product, _ .Quantity = t1.Quantity, _ .PurchaseDate = t1.PurchaseDate, _ .Price = t2.Price, _ .SumPrice = t1.Quantity * t2.Price }
table1.Dump() table2.Dump() table3.Dump() End Sub
Public Class Table1 Inherits List(Of Table1.Item)
Public Class Item Public Sub New(customer As String, country As String, product As String, quantity As Integer, purchaseDate As DateTime) Me.Customer = customer Me.Country = country Me.Product = product Me.Quantity = quantity Me.PurchaseDate = purchaseDate End Sub
Public Customer As String Public Country As String Public Product As String Public Quantity As Integer Public PurchaseDate As DateTime End Class End Class
Public Class Table2 Inherits List(Of Table2.Item)
Public Class Item Public Sub New(country As String, price As Decimal, product As String, startDate As DateTime) Me.Country = country Me.Price = price Me.Product = product Me.StartDate = startDate End Sub
Public Country As String Public Price As Decimal Public Product As String Public StartDate As DateTime End Class End Class
Nur als kleines Anhängsel die C# Version (sieht einfach schöner aus :)).
void Main() { var table1 = new Table1 { new Table1.Item("0815", "Deutschland", "A1", 66, new DateTime(1999, 12, 1)), new Table1.Item("0815", "Deutschland", "A1", 77, new DateTime(2001, 3, 1)), new Table1.Item("0815", "Deutschland", "A1", 88, new DateTime(2005, 3, 1)), new Table1.Item("0815", "Deutschland", "A1", 22, new DateTime(2009, 3, 1)), };
var table2 = new Table2 { new Table2.Item("Deutschland", 10, "A1", new DateTime(1999, 1, 1)), new Table2.Item("Deutschland", 15, "A1", new DateTime(2001, 1, 1)), new Table2.Item("Deutschland", 25, "A1", new DateTime(2009, 1, 1)), };
var table3 = from t1 in table1 let t2 = (from item in table2 where item.Country == t1.Country && item.Product == t1.Product && item.StartDate <= t1.PurchaseDate orderby item.StartDate descending select item).FirstOrDefault() select new { t1.Customer, t1.Country, t1.Product, t1.Quantity, t1.PurchaseDate, t2.Price, SumPrice = t1.Quantity * t2.Price };
table1.Dump(); table2.Dump(); table3.Dump(); }
public class Table1 : List<Table1.Item> { public class Item { public Item() {} public Item(string customer, string country, string product, int quantity, DateTime purchaseDate) { Customer = customer; Country = country; Product = product; Quantity = quantity; PurchaseDate = purchaseDate; }
public string Customer; public string Country; public string Product; public int Quantity; public DateTime PurchaseDate; } }
public class Table2 : List<Table2.Item> { public class Item { public Item() {} public Item(string country, decimal price, string product, DateTime startDate) { Country = country; Price = price; Product = product; StartDate = startDate; }
public string Country; public decimal Price; public string Product; public DateTime StartDate; } }
Hallo Daniel, Sorry für die späte Antwort, aber bei mir war Land unter. Das war genau das was ich gesucht habe. Ich habe es noch ein wenig erweitert um "DefaultIfEmpty". Vielen Dank für die ausführliche Erklärung und die Mühe die Du Dir dabei gegeben hast. Gruß Athu