// C# ResultsetFields fields = new ResultsetFields( 2 ); fields.DefineField( OrderFieldIndex.OrderId, 0, "OrderID" ); fields.DefineField( OrderFieldIndex.OrderDate, 1, "Month" ); fields[1].ExpressionToApply = new DbFunctionCall("CAST({0} AS bigint)", new object[] { new DbFunctionCall("MONTH", new object[] { OrderFields.OrderDate }) });
' VB.NET Dim fields As New ResultsetFields(2) fields.DefineField( OrderFieldIndex.OrderId, 0, "OrderID" ) fields.DefineField( OrderFieldIndex.OrderDate, 1, "Month" ) fields(1).ExpressionToApply = New DbFunctionCall("CAST({0} AS bigint)", _ New Object() { New DbFunctionCall("MONTH", New Object() { OrderFields.OrderDate }) })
// C# ResultsetFields fields = new ResultsetFields(2); fields.DefineField(SupplierFields.Id, 0); fields.DefineField(SupplierFields.Region, 1); fields[1].ExpressionToApply = new DbFunctionCall( "CASE {0} WHEN 'MA' THEN 'Massachusetts' WHEN 'OR' THEN 'Oregon' ELSE 'Unknown' END", new object[] { SupplierFields.Region });
' VB.NET Dim fields As New ResultsetFields(2) fields.DefineField(SupplierFields.Id, 0) fields.DefineField(SupplierFields.Region, 1) fields(1).ExpressionToApply = New DbFunctionCall( _ "CASE {0} WHEN 'MA' THEN 'Massachusetts' WHEN 'OR' THEN 'Oregon' ELSE 'Unknown' END", _ New Object() { SupplierFields.Region })
// C# // create a filter which filters on month equals 3 IPredicate monthFilter = new EntityField("OrderMonth", new DbFunctionCall("CAST({0} AS bigint)", new object[] { new DbFunctionCall("MONTH", new object[] { OrderFields.OrderDate }) })) == 3;
' VB.NET ' create a filter which filters on month equals 3 Dim monthFilter As IPredicate = New EntityField("OrderMonth", New DbFunctionCall("CAST({0} AS bigint)", _ New Object() { New DbFunctionCall("MONTH", New Object() { OrderFields.OrderDate }) })) = 3
// C# // create a filter which filters on month equals 3 IPredicate monthFilter = new EntityField2("OrderMonth", new DbFunctionCall("CAST({0} AS bigint)", new object[] { new DbFunctionCall("MONTH", new object[] { OrderFields.OrderDate }) })) == 3;
' VB.NET ' create a filter which filters on month equals 3 Dim monthFilter As IPredicate = New EntityField2("OrderMonth", New DbFunctionCall("CAST({0} AS bigint)", _ New Object() { New DbFunctionCall("MONTH", New Object() { OrderFields.OrderDate }) })) = 3
-- T-SQL CREATE FUNCTION fn_CalculateOrderTotal(@orderID int, @useDiscounts bit) RETURNS DECIMAL AS BEGIN DECLARE @toReturn DECIMAL IF @useDiscounts=0 BEGIN SELECT @toReturn = SUM(UnitPrice * Quantity) FROM [Order Details] WHERE OrderID = @orderID GROUP BY OrderID END IF @useDiscounts=1 BEGIN SELECT @toReturn = SUM((UnitPrice-Discount) * Quantity) FROM [Order Details] WHERE OrderID = @orderID GROUP BY OrderID END RETURN @toReturn END
The example code below fetches a single row of data all using a function call, using a DynamicList. Assert statements have been left in the code to illustrate the values to expect for the various fields fetched.
// C# ResultsetFields fields = new ResultsetFields( 8 ); fields.DefineField( OrderFields.OrderId, 0, "OrderID" ); fields.DefineField( OrderFields.OrderDate, 1, "Month" ); fields.DefineField( OrderFields.OrderDate, 2, "Year" ); fields.DefineField( OrderFields.OrderDate, 3, "YearPlus4" ); fields.DefineField( OrderFields.OrderDate, 4, "OrderTotalWithDiscounts" ); fields.DefineField( OrderFields.OrderDate, 5, "OrderTotalWithoutDiscounts" ); fields.DefineField( OrderFields.OrderDate, 6, "YearOfGetDate" ); fields.DefineField( OrderFields.OrderDate, 7, "RealOrderDate" ); fields[1].ExpressionToApply = new DbFunctionCall( "MONTH", new object[] { OrderFields.OrderDate } ); fields[2].ExpressionToApply = new DbFunctionCall( "YEAR", new object[] { OrderFields.OrderDate } ); fields[3].ExpressionToApply = new Expression( new DbFunctionCall( "YEAR", new object[] { OrderFields.OrderDate } ), ExOp.Add, 4 ); fields[4].ExpressionToApply = new DbFunctionCall( "dbo", "fn_CalculateOrderTotal", new object[] { OrderFields.OrderId, 1 } ); fields[5].ExpressionToApply = new DbFunctionCall( "dbo", "fn_CalculateOrderTotal", new object[] { OrderFields.OrderId, 0 } ); fields[6].ExpressionToApply = new DbFunctionCall( "YEAR", new object[] { new DbFunctionCall( "GETDATE", null ) } ); DataTable results = new DataTable(); TypedListDAO dao = new TypedListDAO(); dao.GetMultiAsDataTable( fields, results, 0, null, null, null, true, null, null, 0, 0 ); foreach( DataRow row in results.Rows ) { DateTime realOrderDate = (DateTime)row[7]; Assert.AreEqual( (int)row[1], realOrderDate.Month ); Assert.AreEqual( (int)row[2], realOrderDate.Year ); Assert.AreEqual( (int)row[3], realOrderDate.Year + 4 ); Assert.AreEqual( (int)row[6], DateTime.Now.Year ); }
' VB.NET Dim fields As New ResultsetFields( 8 ) fields.DefineField( OrderFields.OrderId, 0, "OrderID" ) fields.DefineField( OrderFields.OrderDate, 1, "Month" ) fields.DefineField( OrderFields.OrderDate, 2, "Year" ) fields.DefineField( OrderFields.OrderDate, 3, "YearPlus4" ) fields.DefineField( OrderFields.OrderDate, 4, "OrderTotalWithDiscounts" ) fields.DefineField( OrderFields.OrderDate, 5, "OrderTotalWithoutDiscounts" ) fields.DefineField( OrderFields.OrderDate, 6, "YearOfGetDate" ) fields.DefineField( OrderFields.OrderDate, 7, "RealOrderDate" ) fields(1).ExpressionToApply = New DbFunctionCall( "MONTH", New Object() { OrderFields.OrderDate } ) fields(2).ExpressionToApply = New DbFunctionCall( "YEAR", New Object() { OrderFields.OrderDate } ) fields(3).ExpressionToApply = New Expression( New DbFunctionCall( "YEAR", New Object() { OrderFields.OrderDate } ), ExOp.Add, 4 ) fields(4).ExpressionToApply = New DbFunctionCall( "dbo", "fn_CalculateOrderTotal", New Object() { OrderFields.OrderId, 1 } ) fields(5).ExpressionToApply = New DbFunctionCall( "dbo", "fn_CalculateOrderTotal", New Object() { OrderFields.OrderId, 0 } ) fields(6).ExpressionToApply = New DbFunctionCall( "YEAR", New Object() { New DbFunctionCall( "GETDATE", Nothing ) } ) Dim results As New DataTable() Dim dao As New TypedListDAO() dao.GetMultiAsDataTable( fields, results, 0, Nothing, Nothing, Nothing, True, Nothing, Nothing, 0, 0 ) For Each row As DataRow in results.Rows Dim realOrderDate As DateTime = CType(row(7), DateTime) Assert.AreEqual( CInt(row(1)), realOrderDate.Month ) Assert.AreEqual( CInt(row(2)), realOrderDate.Year ) Assert.AreEqual( CInt(row(3)), realOrderDate.Year + 4 ) Assert.AreEqual( CInt(row(6)), DateTime.Now.Year ) Next
// C# ResultsetFields fields = new ResultsetFields( 8 ); fields.DefineField( OrderFields.OrderId, 0, "OrderID" ); fields.DefineField( OrderFields.OrderDate, 1, "Month" ); fields.DefineField( OrderFields.OrderDate, 2, "Year" ); fields.DefineField( OrderFields.OrderDate, 3, "YearPlus4" ); fields.DefineField( OrderFields.OrderDate, 4, "OrderTotalWithDiscounts" ); fields.DefineField( OrderFields.OrderDate, 5, "OrderTotalWithoutDiscounts" ); fields.DefineField( OrderFields.OrderDate, 6, "YearOfGetDate" ); fields.DefineField( OrderFields.OrderDate, 7, "RealOrderDate" ); fields[1].ExpressionToApply = new DbFunctionCall( "MONTH", new object[] { OrderFields.OrderDate } ); fields[2].ExpressionToApply = new DbFunctionCall( "YEAR", new object[] { OrderFields.OrderDate } ); fields[3].ExpressionToApply = new Expression( new DbFunctionCall( "YEAR", new object[] { OrderFields.OrderDate } ), ExOp.Add, 4 ); fields[4].ExpressionToApply = new DbFunctionCall( "dbo", "fn_CalculateOrderTotal", new object[] { OrderFields.OrderId, 1 } ); fields[5].ExpressionToApply = new DbFunctionCall( "dbo", "fn_CalculateOrderTotal", new object[] { OrderFields.OrderId, 0 } ); fields[6].ExpressionToApply = new DbFunctionCall( "YEAR", new object[] { new DbFunctionCall( "GETDATE", null ) } ); DataTable results = new DataTable(); using( DataAccessAdapter adapter = new DataAccessAdapter() ) { adapter.FetchTypedList( fields, results, null ); } foreach( DataRow row in results.Rows ) { DateTime realOrderDate = (DateTime)row[7]; Assert.AreEqual( (int)row[1], realOrderDate.Month ); Assert.AreEqual( (int)row[2], realOrderDate.Year ); Assert.AreEqual( (int)row[3], realOrderDate.Year + 4 ); Assert.AreEqual( (int)row[6], DateTime.Now.Year ); }
' VB.NET Dim fields As New ResultsetFields( 8 ) fields.DefineField( OrderFields.OrderId, 0, "OrderID" ) fields.DefineField( OrderFields.OrderDate, 1, "Month" ) fields.DefineField( OrderFields.OrderDate, 2, "Year" ) fields.DefineField( OrderFields.OrderDate, 3, "YearPlus4" ) fields.DefineField( OrderFields.OrderDate, 4, "OrderTotalWithDiscounts" ) fields.DefineField( OrderFields.OrderDate, 5, "OrderTotalWithoutDiscounts" ) fields.DefineField( OrderFields.OrderDate, 6, "YearOfGetDate" ) fields.DefineField( OrderFields.OrderDate, 7, "RealOrderDate" ) fields(1).ExpressionToApply = New DbFunctionCall( "MONTH", New Object() { OrderFields.OrderDate } ) fields(2).ExpressionToApply = New DbFunctionCall( "YEAR", New Object() { OrderFields.OrderDate } ) fields(3).ExpressionToApply = New Expression( New DbFunctionCall( "YEAR", New Object() { OrderFields.OrderDate } ), ExOp.Add, 4 ) fields(4).ExpressionToApply = New DbFunctionCall( "dbo", "fn_CalculateOrderTotal", New Object() { OrderFields.OrderId, 1 } ) fields(5).ExpressionToApply = New DbFunctionCall( "dbo", "fn_CalculateOrderTotal", New Object() { OrderFields.OrderId, 0 } ) fields(6).ExpressionToApply = New DbFunctionCall( "YEAR", New Object() { New DbFunctionCall( "GETDATE", Nothing ) } ) Dim results As New DataTable() Dim adapter As New DataAccessAdapter() Try adapter.FetchTypedList( fields, results, Nothing ) Finally adapter.Dispose() End Try For Each row As DataRow in results.Rows Dim realOrderDate As DateTime = CType(row(7), DateTime) Assert.AreEqual( CInt(row(1)), realOrderDate.Month ) Assert.AreEqual( CInt(row(2)), realOrderDate.Year ) Assert.AreEqual( CInt(row(3)), realOrderDate.Year + 4 ) Assert.AreEqual( CInt(row(6)), DateTime.Now.Year ) Next