DeepData with Linq in C#
Old forum URL: forums.lhotka.net/forums/t/5756.aspx
alef posted on Thursday, November 06, 2008
When I try to run the DeepData example with Linq it fails when executing the following line of code
data = ((DeepData.DTO.
OrderDto[])(dal.GetOrders()));
the following error is raised
Unable to cast object of type Csla.Data.SafeDataReader to type DeepData.DTO.OrderDto
In App.Config I added the following:
<
add key="OrderData" value="DeepData.DAL.DLinq.OrderData,DeepData.DAL.DLinq"/>
alef replied on Friday, November 07, 2008
I've found partially a solution. With the modifications below the grid with the orders will be filled in.
But the other grids with LineItems and LineItemDetails stay still empty. I'm alreday 50% happy.
dal.GetOrders must return an object (see abstract method in DeepData.DAL : public abstract object GetOrders())
So GetOrders will cast the SafeDataReader to an object.
And in FetchDto we can cast back the object to a SafeDataReader.
Apparently this is not a problem. But converting a
Csla.Data.SafeDataReader to a DeepData.DTO.OrderDto doesn't want to
work in C#, but in VB it is not a problem.
Can somebody explain this a little bit? I want to understand the differences between VB and C#.
Below the code modifications:
Project DeepData.DAL.DLinq.csproj
class : OrderData.cs
public override object GetOrders()
{
var q = from o in db.Orders select o;
IDbCommand command = db.GetCommand(q);
command.Connection = db.Orders.Context.Connection;
command.Connection.Open();
SafeDataReader reader = new
SafeDataReader(command.ExecuteReader(CommandBehavior.CloseConnection));
return reader;
}
Project DeepData.Library.csproj
class OrderList.da.cs
private void FetchDto()
{
try
{
RaiseListChangedEvents = false;
IsReadOnly = false;
DeepData.DAL.DataFactory df = new DeepData.DAL.DataFactory();
SafeDataReader data;
using (var dal = df.GetOrderDataObject())
{
data = new SafeDataReader((IDataReader)dal.GetOrders());
while (data.Read())
Add(OrderInfo.GetOrderInfo(data));
}
}
finally
{
IsReadOnly = true;
RaiseListChangedEvents = true;
}
}
The other grids with LineItems and LineItemDetails stay empty.
Is the reason maybe the following:
When I compare the VB version and the C# version of DeepData I remark that in VB you have the following classes:
OrderInfo.da.vb
LineItemInfo.da.vb
LineItemDetailInfo.da.vb
but these classes are missing in C#
Copyright (c) Marimer LLC