Typed DataSet from xsd using LINQ. Trial by fire
If you are on this particular page, then the question you might be asking your self is this:
How come that when I try to query a typed dataset that is created by using the xsd.exe tool via LINQ, I’ll get a compilation error along the lines of:
'ColumnName' is not a member of 'System.Data.DataRow'
or
"Cannot convert lambda expression to type 'string' because it is not a delegate type"
Well, there is hopefully a short answer to the question. You have not included the /enableLinqDataSet (or /eld for short) when you are building the class for the typed dataset.
Since I do not have the way of words I’ll show this step by step, my preferred way J
Create a new C# console application called ELD in C:\Temp
Right click the created Project and add a new XML Schema (.xsd) and call it EldDataSet.xsd
Replace the generated XML with:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="MyDataset" xmlns="" xmlns:xs="https://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="EldDataSet" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="TheTable">
<xs:complexType>
<xs:sequence>
<xs:element name="TheColumn" type="xs:int" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
Save it. Now you should have EldDataSet.xsd in C:\Temp\Eld\Eld
Now it is time to create the class for the typed DataSet. So start a Visual Studio 2008 Command Prompt and navigate to C:\Temp\Eld\Eld
We will create two dll files, one built with the /ELD switch, one built without the /ELD switch.
So let’s start with the one using the /ELD switch:
xsd.exe /d /l:CS EldDataSet.xsd /eld /n:WithEld
And then build the dll.
csc.exe /out:WithEld.dll /t:library EldDataSet.cs /r:System.dll /r:System.Data.dll
Now do the one without the switch:
xsd.exe /d /l:CS EldDataSet.xsd /n:WithOutEld
And then build the dll.
csc.exe /out:WithOutEld.dll /t:library EldDataSet.cs /r:System.dll /r:System.Data.dll
(If you are using VB, then of course you have to use the VB compiler, vbc.exe, instead of the C# one, csc.exe. See below for all possible compiler settings.)
Now you should have WithEld.dll and WithOutEld.dll in C:\Temp\Eld\Eld
So go back to VS and delete EldDataSet.xsd from the project.
Add references to WithEld.dll and WithOutEld.dll
Replace the main method so it looks, for example, as follows:
static void Main(string[] args)
{
//Using DS compiled with /ELD, this works
WithEld.EldDataSet withEldDs = new WithEld.EldDataSet();
var v1 = from x in withEldDs.TheTable select x.TheColumn;
//Using DS compiled without /ELD, this does not work
WithOutEld.EldDataSet withOutEldDs = new WithOutEld.EldDataSet();
var v2 = from y in withOutEldDs.TheTable select y.TheColumn;
}
This will give:
"Cannot convert lambda expression to type 'string' because it is not a delegate type"
If you are using a VB client, then for example, this:
Sub Main()
'Using DS compiled with /ELD, this works
Dim withEldDs As WithEld.EldDataSet = New WithEld.EldDataSet
Dim list1 As List(Of Integer) = (From y In withEldDs.TheTable Select y.TheColumn Distinct).ToList()
'Using DS compiled without /ELD, this does not work
Dim withOutEldDs As WithOutEld.EldDataSet = New WithOutEld.EldDataSet
Dim list2 As List(Of Integer) = (From x In withOutEldDs.TheTable Select x.TheColumn Distinct).ToList()
End Sub
"'TheColumn' is not a member of 'System.Data.DataRow'."
So the point here really is that if you are to use LINQ queries against your typed dataset, then you must use the /ELD switch when running xsd.exe.
Note that this switch in not in .Net 2.0.
Oh, if you wonder about the trial by fire bit, well, ELD is the Swedish word for fire, simple as that J
[.Net 2.0] "XML Schema Definition Tool (Xsd.exe)"
https://msdn.microsoft.com/en-us/library/x6c1kb0s(VS.80).aspx
[.Net 3.5] "XML Schema Definition Tool (Xsd.exe)"
https://msdn.microsoft.com/en-us/library/x6c1kb0s.aspx
"C# Language Reference - C# Compiler Options Listed Alphabetically"
https://msdn.microsoft.com/en-us/library/6ds95cz0.aspx
"Visual Basic Language Reference - Visual Basic Compiler Options Listed Alphabetically"
https://msdn.microsoft.com/en-us/library/w95cx9k1.aspx
Comments
- Anonymous
February 05, 2009
PingBack from http://www.anith.com/?p=6013