Bonjour,
Ma requête linq me génère le type d'exception indiqué en titre.
Je vous ai mis le contenu complet du dataset dont je me sers pour illustrer mon problème, afin que vous ayez tous les éléments pour essayer de regénérer éventuellement mon problème ou voir sinon directement ce qui ne va pas.
Le code complet, avec les données de tests (équivalentes à ce que j'ai en base) qui font que ça plante, est également fourni ci-dessous.
L'exception arrive à l'intérieur de mon foreach, au moment où il tente de gérer le contenu de ma 1ère requête(queryWithxxxNotNull). Dans mon callStack, l'eception est levée au niveau du b.xxx de
equals new {
b.xxx,
b.zzz
}
Le detail de mon erreur est ceci:
Quand je regarde en mode débug le contenu de "Results View" de queryWithxxxNotNull, j'ai déjà ce message en guise de Value.The value for column 'xxx' in table 'TestDetail' is DBNull.
De plus, xxx est un champs int qui dans ma base peut être nul mais qui via le designer ne peut pas être setté à nul.
ds.xsd:
Remplissage de mes datatable et requêtage sur celles-ci:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 <?xml version="1.0" encoding="utf-8"?> <xs:schema id="ds" targetNamespace="http://tempuri.org/ds.xsd" xmlns:mstns="http://tempuri.org/ds.xsd" xmlns="http://tempuri.org/ds.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop" attributeFormDefault="qualified" elementFormDefault="qualified"> <xs:annotation> <xs:appinfo source="urn:schemas-microsoft-com:xml-msdatasource"> <DataSource DefaultConnectionIndex="0" FunctionsComponentName="QueriesTableAdapter" Modifier="AutoLayout, AnsiClass, Class, Public" SchemaSerializationMode="IncludeSchema" xmlns="urn:schemas-microsoft-com:xml-msdatasource"> <Connections /> <Tables /> <Sources /> </DataSource> </xs:appinfo> </xs:annotation> <xs:element name="ds" msdata:IsDataSet="true" msdata:UseCurrentLocale="true" msprop:Generator_UserDSName="ds" msprop:Generator_DataSetName="ds" msprop:EnableTableAdapterManager="true"> <xs:complexType> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element name="Test" msprop:Generator_UserTableName="Test" msprop:Generator_RowDeletedName="TestRowDeleted" msprop:Generator_RowChangedName="TestRowChanged" msprop:Generator_RowClassName="TestRow" msprop:Generator_RowChangingName="TestRowChanging" msprop:Generator_RowEvArgName="TestRowChangeEvent" msprop:Generator_RowEvHandlerName="TestRowChangeEventHandler" msprop:Generator_TableClassName="TestDataTable" msprop:Generator_TableVarName="tableTest" msprop:Generator_RowDeletingName="TestRowDeleting" msprop:Generator_TablePropName="Test"> <xs:complexType> <xs:sequence> <xs:element name="xxx" msprop:Generator_UserColumnName="xxx" msprop:nullValue="_throw" msprop:Generator_ColumnPropNameInRow="xxx" msprop:Generator_ColumnPropNameInTable="xxxColumn" msprop:Generator_ColumnVarNameInTable="columnxxx" type="xs:int" minOccurs="0" /> <xs:element name="yyy" msprop:Generator_UserColumnName="yyy" msprop:nullValue="_null" msprop:Generator_ColumnVarNameInTable="columnyyy" msprop:Generator_ColumnPropNameInTable="yyyColumn" msprop:Generator_ColumnPropNameInRow="yyy" type="xs:string" minOccurs="0" /> <xs:element name="zzz" msprop:Generator_UserColumnName="zzz" msprop:Generator_ColumnVarNameInTable="columnzzz" msprop:Generator_ColumnPropNameInRow="zzz" msprop:Generator_ColumnPropNameInTable="zzzColumn" type="xs:int" minOccurs="0" /> <xs:element name="aaa" msprop:Generator_UserColumnName="aaa" msprop:Generator_ColumnVarNameInTable="columnaaa" msprop:Generator_ColumnPropNameInRow="aaa" msprop:Generator_ColumnPropNameInTable="aaaColumn" type="xs:string" minOccurs="0" /> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="TestDetail" msprop:Generator_UserTableName="TestDetail" msprop:Generator_RowDeletedName="TestDetailRowDeleted" msprop:Generator_RowChangedName="TestDetailRowChanged" msprop:Generator_RowClassName="TestDetailRow" msprop:Generator_RowChangingName="TestDetailRowChanging" msprop:Generator_RowEvArgName="TestDetailRowChangeEvent" msprop:Generator_RowEvHandlerName="TestDetailRowChangeEventHandler" msprop:Generator_TableClassName="TestDetailDataTable" msprop:Generator_TableVarName="tableTestDetail" msprop:Generator_RowDeletingName="TestDetailRowDeleting" msprop:Generator_TablePropName="TestDetail"> <xs:complexType> <xs:sequence> <xs:element name="xxx" msprop:Generator_UserColumnName="xxx" msprop:nullValue="_throw" msprop:Generator_ColumnPropNameInRow="xxx" msprop:Generator_ColumnPropNameInTable="xxxColumn" msprop:Generator_ColumnVarNameInTable="columnxxx" type="xs:int" minOccurs="0" /> <xs:element name="yyy" msprop:Generator_UserColumnName="yyy" msprop:nullValue="_null" msprop:Generator_ColumnVarNameInTable="columnyyy" msprop:Generator_ColumnPropNameInTable="yyyColumn" msprop:Generator_ColumnPropNameInRow="yyy" type="xs:string" minOccurs="0" /> <xs:element name="zzz" msprop:Generator_UserColumnName="zzz" msprop:Generator_ColumnVarNameInTable="columnzzz" msprop:Generator_ColumnPropNameInRow="zzz" msprop:Generator_ColumnPropNameInTable="zzzColumn" type="xs:int" minOccurs="0" /> <xs:element name="bbb" msprop:Generator_UserColumnName="bbb" msprop:Generator_ColumnVarNameInTable="columnbbb" msprop:Generator_ColumnPropNameInRow="bbb" msprop:Generator_ColumnPropNameInTable="bbbColumn" type="xs:string" minOccurs="0" /> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="Final" msprop:Generator_UserTableName="Final" msprop:Generator_RowDeletedName="FinalRowDeleted" msprop:Generator_RowChangedName="FinalRowChanged" msprop:Generator_RowClassName="FinalRow" msprop:Generator_RowChangingName="FinalRowChanging" msprop:Generator_RowEvArgName="FinalRowChangeEvent" msprop:Generator_RowEvHandlerName="FinalRowChangeEventHandler" msprop:Generator_TableClassName="FinalDataTable" msprop:Generator_TableVarName="tableFinal" msprop:Generator_RowDeletingName="FinalRowDeleting" msprop:Generator_TablePropName="Final"> <xs:complexType> <xs:sequence> <xs:element name="aaa" msprop:Generator_UserColumnName="aaa" msprop:Generator_ColumnVarNameInTable="columnaaa" msprop:Generator_ColumnPropNameInRow="aaa" msprop:Generator_ColumnPropNameInTable="aaaColumn" type="xs:string" minOccurs="0" /> <xs:element name="bbb" msprop:Generator_UserColumnName="bbb" msprop:Generator_ColumnVarNameInTable="columnbbb" msprop:Generator_ColumnPropNameInRow="bbb" msprop:Generator_ColumnPropNameInTable="bbbColumn" type="xs:string" minOccurs="0" /> </xs:sequence> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> </xs:element> </xs:schema>
Merci beaucoup si vous pouviez m'indiquer ce que je ne fais pas correctement. J'ai mis toutes les infos a priori afin que vous puissiez reproduire le problème. S'il manque quelque chose, n'hésitez pas à me le dire.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93 DataRow dr; ds.TestDataTable dt = new ds.TestDataTable(); dr = dt.NewRow(); dr["xxx"] = 1; dr["yyy"] = DBNull.Value; dr["zzz"] = 3; dr["aaa"] = "val1"; dt.AddTestRow((ds.TestRow)dr); dr = dt.NewRow(); dr["xxx"] = DBNull.Value; dr["yyy"] = 2; dr["zzz"] = 3; dr["aaa"] = "val11"; dt.AddTestRow((ds.TestRow)dr); ds.TestDataTable dtWithxxxNotNull = new ds.TestDataTable(); ds.TestRow[] tr1 = (ds.TestRow[])dt.Select("xxx is not null"); if (tr1.Count<ds.TestRow>() > 0) { DataTable looselyTypedDT1 = tr1.CopyToDataTable<ds.TestRow>(); dtWithxxxNotNull.Merge(looselyTypedDT1, true); } ds.TestDataTable dtWithxxxNull = new ds.TestDataTable(); ds.TestRow[] tr2 = (ds.TestRow[])dt.Select("xxx is null"); if (tr2.Count<ds.TestRow>() > 0) { DataTable looselyTypedDT1 = tr2.CopyToDataTable<ds.TestRow>(); dtWithxxxNull.Merge(looselyTypedDT1, true); } ds.TestDetailDataTable dtdetail = new ds.TestDetailDataTable(); dr = dtdetail.NewRow(); dr["xxx"] = 1; dr["yyy"] = DBNull.Value; dr["zzz"] = 3; dr["bbb"] = "val101"; dtdetail.AddTestDetailRow((ds.TestDetailRow)dr); dr = dtdetail.NewRow(); dr["xxx"] = DBNull.Value; dr["yyy"] = 2; dr["zzz"] = 3; dr["bbb"] = "val999"; dtdetail.AddTestDetailRow((ds.TestDetailRow)dr); var queryWithxxxNotNull = from a in dtWithxxxNotNull join b in dtdetail on new { a.xxx, a.zzz } equals new { b.xxx, b.zzz } select new { aaa = a.aaa, bbb = b.bbb }; var queryWithxxxNull = from a in dtWithxxxNull join b in dtdetail on new { a.yyy, a.zzz } equals new { b.yyy, b.zzz } select new { aaa = a.aaa, bbb = b.bbb }; var final_query = queryWithxxxNotNull.Union(queryWithxxxNull); ds.FinalDataTable fdt = new ds.FinalDataTable(); foreach (var item in final_query) { fdt.AddFinalRow(item.aaa, item.bbb); }
Julien
Partager