RDLC Expression Extension Methods
Asked Answered
J

4

9

Is it possible to write extension methods for expressions behind RDLC fields?

For example, let's say that I have a DateTime field in my datasource that may either have a valid value or may be null. I drag and drop a TextBox onto my RDLC and format its value using the ToShortDateString() method. This works fine for populated DateTime value, but this will also obviously throw an exception at runtime if I try to do a .ToShortDateString() on a NULL field.

I was wondering if I could write an extension method that I could use in my RDLC expressions so that when I'm dealing with ?DateTime values, I could call a method like .ConvertFromNullToEmptyString().

Of course there are other ways to work around this issue, but I was wondering if extension methods for use in RDLC expressions would be a possible approach to my business problem.

Thanks folks!

Jarv answered 2/7, 2010 at 23:17 Comment(3)
+1: Good question. I believe so but never had time to figure out. I hope someone answers!Mydriatic
I'm guessing this simply is not possible; too bad - I was looking forward to rewarding a bounty based on a conclusive answer here.Graphics
are you using ssrs, or the asp.net report viewer control. I ask because an RDL file is an ssrs report definition language file, while an RDLC is a report definition client language file, which is used by asp.net to display a report, sans ssrsLyndel
W
2

Yes, this is possible. You can either embed code directly in the report or include a custom assembly.

Winner answered 19/9, 2010 at 8:19 Comment(2)
Those aren't examples of using extension methods - which is what the question is asking...Graphics
@DanP: while this doesn't add extension methods, calling the code is the done in the same way as the normal expressions.Lyndel
A
2

It is possible to use extension methods, but not AS extension methods on an instance of an object. You would have to call them as a static method call on the type of which they are a member. So instead of myDictionary.Values.Sum() -- calling the Sum method on the Values property of a dictionary instance -- you could use System.Linq.Enumerable.Sum(myDictionary.Values) -- passing the instance into the static Sum method of the Enumerable type (in this example, the report must reference the System.Core assembly). So yes, you can use methods that are also extensions, but (it appears anyway) not as extensions on a particular instance.

Adigranth answered 29/2, 2012 at 17:45 Comment(0)
L
0

While I agree with Corina on the solution to the question, I believe a better solution can be reached without going the route she suggests, using built in expressions. In any case where you have a DateTime coming from SQL, you're correct, it can be null, however, you can easily test for this using an IIF statement (remember that the expressions are basically in VB) to check for null / nothing / empty and as long as it is something, run the desired operation, otherwise return blank. Just be careful, as the resulting type of the IIF will probably be string.

Lyndel answered 21/9, 2010 at 22:20 Comment(3)
While I can't spreak for the OP or AMissico; I'm personally interested in extension methods specifically. It is already very well known how to use arbitrary custom code in a report - so answers along those lines are somewhat pointless...Graphics
One of the challenges with what you're asking is that reflection is probably being used to execute the method, so if you call .ConvertFromNullToEmptyString() on the null object (null datetime), you'll get a null reference exception regardless, unless you rewrote the DateTime? object to have a static method called ConvertFromNullToEmptyString which would provide the functionality you're asking for. At the moment, I don't see how this particular piece of functionality benefits you more than a some custom code.Lyndel
@Lyndel I just post an answer covering that issue. It uses the If() ternary operator instead of the IIf() function.Heptateuch
H
0

For example, let's say that I have a DateTime field in my datasource that may either have a valid value or may be null. I drag and drop a TextBox onto my RDLC and format its value using the ToShortDateString() method. This works fine for populated DateTime value, but this will also obviously throw an exception at runtime if I try to do a .ToShortDateString() on a NULL field.

There should be no need for a custom function in your case. Just use VB's If() ternary operator:

=If(Fields!MyDate.Value IsNot Nothing, Fields!MyDate.Value.ToShortDateString(), "N/A")

(Personally, I funnel my objects through AutoMapper and let it substitute null values with default values or objects, so that I don't have to deal with null values in the report at all).


It should be mentioned, that the If(condition, true_part, false_part) ternary operator should (with one I) be preferred over the IIf(condition, true_part, false_part) function (with two I's) in most cases.

The If() ternary operator will short-circuit evaluate only the part that corresponds to condition outcome (e.g. a condition that is True will only evaluate the true_part).

The IIf() function will always evaluate the condition and both parts, because it is just a function call and all parameters of the function will be evaluated before the call.

Heptateuch answered 6/3, 2021 at 23:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.