Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Der Beginn meines Programms startet einen Integrationstest, der über einen Tupel zwei Wertpapierbestandslisten vergleicht.
Die erste Liste enthält die Wertpapiere einer eingelesenen csv – Datei eines Depots.
Die zweite Liste enthält die Wertpapiere die bereits auf dem SQL – Server abgelegt sind.
Es werden die Wertpapiere in den beiden Listen gezählt und die hinzugekommene(n) in einem WPF – Fenster mit ISIN und Wertpapiername aufgelistet
Eine Hilfsklasse IsinUndBestand enthält die Properties ISIN, Wertpapiername und Bestand.
Die Linq – Abfrage gibt mit Bestand = 0 die im Depot oder in der Serverdatenbank hinzugekommenen Wertpapiere nach ISIN und Wertpapiername richtig aus.
dbContent = anlagenContext.Wertpapiere.Where(w => w.Anlagenklassen.Anlagenklasse_DepotbankID == selectedBank.DepotbankID).Select(w => new IsinUndBestand { WertpapierName = w.WpName, ISIN = w.ISIN, Bestand = 0 }).ToList(); // ISIN- und Bestandsabfrage
return Typpruefung(csvContent, dbContent);
Soweit so gut funktioniert das.
Jetzt möchte ich mit einem weiteren Tupel einen Bestandscheck durchführen. Es sollen die Abweichungen der Bestände(Stückzahlen) verglichen werden.
Dazu habe ich die Linq – Abfrage geändert um die Bestände auszugeben.

dbContent = anlagenContext.Depotkurse.Select(b => new IsinUndBestand { WertpapierName = b.Wertpapiere.WpName, Bestand = b.Bestand }).ToList();

Problem: Es werden alle Wertpapiere aller 3 verschiedenen Depots ausgegeben und nicht nur des ausgewählten Depots.
Dabei zeigt der Debugger
bei csvContent richtig 18 Wertpapiere mit den richtigen Beständen(Stückzahlen) an
und
bei dbContent zu viele 39888 einzelne Wertpapiere mit den richtigen Beständen(Stückzahlen) an.
Hier bliebt beim Ansehen von dbContent die Spalte IsinUndBestand.ISIN leer.
Es fehlt der Zugriff auf das ausgewählte Depot, das ich mit folgender zusätzlichen Linq – Routine einbringen will.

dbContent = ((List<isinundbestand>)(from f in anlagenContext.Depotbanken
                                              where f.Anlagenklassen == SelectedDepotbank.SelectedValue
                                              select f));
return Typpruefung(csvContent, dbContent);

Ein Ausnahmefehler beendet die Auflistung.
System.InvalidCastException: "Das Objekt des Typs "System.Data.Entity.Infrastructure.DbQuery`1[ETFsSparplan.Entities.Depotbank]" kann nicht in Typ "System.Collections.Generic.List`1[ETFsSparplan.Hilfsklassen.IsinUndBestand]" umgewandelt werden."
Unbehandelte Ausnahme:
Das Objekt des Typs "System.Data.Entity.Infrastructure.DbQuery`1[ETFsSparplan.Entities.Depotbank]" kann nicht in Typ "System.Collections.Generic.List`1[ETFsSparplan.Hilfsklassen.IsinUndBestand]" umgewandelt werden.
Hier stecke ich seit Wochen fest-
Kann mir jemand bitte die Linq-Routine entsprechend anpassen. Ich weiß nicht wie ich eine entsprechende Typkonvertierung vornehmen kann.
Solltest Du weitere Informationen brauchen, lasse ich diese Dir gerne zukommen.


Google Translate:
The beginning of my program starts an integration test that compares two lists of securities via a tuple.
The first list contains the securities of a read csv file of a securities account.
The second list contains the securities that are already stored on the SQL server.
The securities in the two lists are counted and the new one(s) are listed in a WPF window with ISIN and security name
A helper class IsinAndHold contains the properties ISIN, Security Name and Holding.
With Stock = 0, the Linq query correctly outputs the securities added to the depot or the server database according to ISIN and security name.
dbContent = anlagenContext.Wertpapiere.Where(w => w.Anlagenklasse.Anlagenklasse_DepotbankID == selectedBank.DepotbankID).Select(w => new IsinUndBestand { SecuritiesName = w.WpName, ISIN = w.ISIN, inventory = 0 }).ToList (); // ISIN and stock query
return Type Check(csvContent, dbContent);
So far so good it works.
Now I want to do an inventory check with another tuple. The deviations of the stocks (number of pieces) are to be compared.
For this I changed the Linq query to output the stocks.
dbContent = anlagenContext.Depotpreise.Select(b => new IsinUndBestand { SecuritiesName = b.Securities.WpName, Stock = b.Stock }).ToList();

Problem: All securities of all 3 different depots are issued and not just the selected depot.
The debugger shows
with csvContent correctly 18 securities with the correct stocks (numbers).
and
in dbContent too many 39888 individual securities with the correct stocks (numbers).
Here, when viewing dbContent, the IsinUndBestand.ISIN column remains empty.
Access to the selected depot is missing, which I want to bring in with the following additional Linq routine.
dbContent = ((List<isinundstand>)(from f in anlagenContext.Depotbanken
where f.Asset Classes == SelectedDepotbank.SelectedValue
select f));
return Type Check(csvContent, dbContent);

An unhandled exception terminates the collection.
System.InvalidCastException: "The object of type 'System.Data.Entity.Infrastructure.DbQuery`1[ETFsSparplan.Entities.Depotbank]' cannot be cast to type 'System.Collections.Generic.List`1[ETFsSparplan.Hilfs Klassen.IsinUndBestand] " being transformed."
Unhandled exception:
The object of type "System.Data.Entity.Infrastructure.DbQuery`1[ETFsSparplan.Entities.Depotbank]" cannot be cast to type "System.Collections.Generic.List`1[ETFsSparplan.Hilfs Klassen.IsinUndBestand]".
I've been stuck here for weeks-
Can someone please adjust the linq routine accordingly. I don't know how to do an appropriate type conversion.
If you need any further information, I'll be happy to send it to you.


What I have tried:

C#
var dbContent = (
    (List<IsinUndBestand>)(from f in anlagenContext.Depotbanken
                           where f.Anlagenklassen == 
                           SelectedDepotbank.SelectedValue
                           select f));

var dbContent = anlagenContext.Depotkurse
	.Select(b => new IsinUndBestand
		{
			WertpapierName = b.Wertpapiere.WpName,
			Bestand = b.Bestand 
		}
	).ToList();

return Typpruefung(csvContent, dbContent);
Posted
Updated 19-May-23 23:53pm
v4

1 solution

Being a Linq statement, it will not execute against the database until you start iterating over the results or calling a ToList(). This is why you are getting the System.InvalidCastException.
 
Share this answer
 
Comments
Hans-Jörg Eitner 20-May-23 6:13am    
Danke für die schnelle Antwort.
Ich habe die beiden Linq Abfragen abgeändert und es erscheint der gleiche Ausnahmefehler.

System.InvalidCastException: "Das Objekt des Typs "System.Data.Entity.Infrastructure.DbQuery`1[ETFsSparplan.Entities.Depotbank]" kann nicht in Typ "System.Collections.Generic.List`1[ETFsSparplan.Hilfsklassen.IsinUndBestand]" umgewandelt werden."


Hans Jörg
Graeme_Grant 20-May-23 6:43am    
ToList() will create a regular List, not as you describe. Please check again.
Hans-Jörg Eitner 30-May-23 7:33am    
Ich habe die Linq - Abfrage aufgeteilt und komme einfach nicht weiter. Ich bin Anfänger und noch nicht routinierter Programmierer.

IsinUndBestand IsinUndBestand = new IsinUndBestand();

dbContent = new List<isinundbestand>();

using (AnlagenklasseContext AnlagenklasseContext = new AnlagenklasseContext())

selectedBank = SelectedDepotbank.SelectedValue;

var abfrage1 = from f in anlagenContext.Depotbanken
where f.Anlagenklassen == SelectedDepotbank.SelectedValue
select f;

var abfrage2 = from w in abfrage1
where w.Wertpapier == anlagenContext.Wertpapiere
select w;

var abfrage3 = from k in abfrage2
where k.Depotkurs == anlagenContext.Depotkurse
select k;

List dbcontent = (List<isinundbestand>)from m in abfrage3
where m.Depotkurs == anlagenContext.Depotkurse
select (b => new IsinUndBestand(List<dbcontent>)
{
ISIN = w.Wertpapiere.ISIN,
WertpapierName = w.Wertpapiere.WpName,
Bestand = k.Depotkurse.Bestand = Convert.ToDecimal(b.Bestand)}).ToList();

"Der Debugger bemängelt die letzte select-Anweisung mit (Erweiterung) IQueryable<depotbank> IQueryable <tresult> Select <depotbank ,="" tresult="">(Expression <func <depotbank="">, TResult> > selector) + 3 Überladungen".
Kann jemand mit dieser Meldung etwas anfangen und meine Select - Anweisung richtig stellen?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900