I'm using VB.Net and I would like to know how to get the selected checkboxes in a checkboxlist using linq and lambda syntax (not query syntax, repeat NO query syntax).
I tried this but it's definitely not right.
cblRequired.Items.OfType(Of ListItem).Where(Function (i As ListItem ) i.Selected End Function)
I believe that the only thing wrong with your code is that you should not have the End Function, since it's a single-line lambda expression. This should work:
cblRequired.Items.OfType(Of ListItem).Where(Function(i As ListItem) i.Selected)
Technically, you don't need to specify the type of i, since it will automatically infer the type:
cblRequired.Items.OfType(Of ListItem).Where(Function(i) i.Selected)
If you want it to be a multi-line lamba expression, that would look like this:
cblRequired.Items.OfType(Of ListItem).Where(Function(i)
Return i.Selected
End Function)
Related
I have a simple query:
dim res = (From x In db.INVENTORies
Where x.INVENTORY_ACTIVATION_DATE IsNot Nothing).count
I need to run this for 14 different date fields, so how do I run this where INVENTORY_ACTIVATION_DATE can be a variable/dynamic?
something like:
function GetCount(aField as string) as integer
return (From x In db.INVENTORies
Where x.**<Use aField here>** IsNot Nothing).count
end function
Thanks in advance.
Steve
I've tried Dynamic Linq and PredicateBuilder, but there are no good examples for my scenario.
C# Eval Expression
Disclaimer: I'm the owner of the project C# Eval Expression
The library is not free, but you can do pretty much any dynamic LINQ using the same syntax as C#.
So it can be used in your VB.NET application, but you will need to use C# syntax for the dynamic part
return context.Customers.WhereDynamic("x => x.AnyField != null && x.AnyField2 != null").count
As you can see, you simply need to create the string to execute dynamically.
LINQ Dynamic
https://www.nuget.org/packages/System.Linq.Dynamic.Core/
The same can be achieved with LINQ Dynamic. You can find some where example here
You don't have to use the PredicateBuilder but simply create the string dynamically.
Through much trail and error, I finally got this working. I ended up using System.Linq.Dynamic.Core.
I need to provide my RDL files to teammates so that they can make minor customizations for each client. One of the ways I thought I might improve the efficiency is if I could build more complex expressions inside of custom code functions so that they can input some simple arguments and have the function handle the "heavy lifting" of adjusting the expression accordingly.
This is a very simple example, and not one I would take this step for, but I thought it the easiest place to start figuring out if I can make this work. For instance, in a tablix we want a count returned based on a value where the value is customized per client (and isn't a parameter).
=Count(iif(trim(Fields!Category.Value)="OPTIONA",1,nothing))
Is there a way I could build a function so that my teammates would just need to enter the following?
=Code.CustomFunction("OPTIONA")
My understanding is that the custom code in Report Builder can't query datasets, or at least not in the way that an expression within a tablix would be able to. I've built custom functions that work with the results of an expression added as the argument, but I can't seem to wrap my head around if there's a way to construct an expression within a custom function and pass it back to the expression.
For instance:
Public Function CustomFunction(field As String) As String
Dim customExpression As String = "Count(iif(trim(Fields!Category.Value)=" & field & ",1,nothing))"
Return customExpression
End Function
As expected, this just returns a string with the text of the expression, but not an executed expression. Is what I'm trying to achieve possible with Report Builder?
Or, as an alternative approach, can I somehow place variables at the beginning of an expression that are used later so that anyone else working on the expression just needs to worry about the beginning? Essentially create multiple custom functions and call them later on?
=Code.CustomFunction("OPTIONA",Count(iif(trim(Fields!Category.Value)=Code.CustFunc01(),1,nothing)))
Honestly not sure how I would go about building the functions themselves from here.
You can use a function instead of the related field. The function takes the field string as an argument and the filter string for which will increase the counter. Finally it returns the original field value
Private Dim Counter As Integer
Public Function SetCounter( Expr As String, Filter As String) As String
If Expr = Filter Then Counter = Counter + 1
Return Expr
End Function
Public Function GetCounter( ) As Integer
Return Counter
End Function
For the field value you can use the following expression (yellow color)
=Code.SetCounter( Fields!MyString.Value,"OPTION A")
To get the counter value you can either use the following expression calling a function (orange color)
= Code.GetCounter()
Or make the variable public and use Code.Counter as the expression
i have a populated list:
def someList=... (string values)
and I want to pass this into a SQL statement to restrict which columns the query selects.
db.rows("select ${someList} from arch_application")
However, I get this error when I try to do so:
There is a ? parameter in the select list. This is not allowed.
Anyone have an ideas? Thanks!
When you pass a GString to Sql.rows, it gets parsed differently than normal in groovy. In particular, it creates a PreparedStatement with replaceable parameters for ${} substitutions. In your case this is probably not what you want. Try forcing the GString to a Java string:
db.rows("select ${someList.join(',')} from arch_application" as String)
I have a method that takes an System.Action, this is what I'm trying to feed it:
Function() Me._existingImports = Me.GetImportedAds()
The thing is that it complains about the = sign since it thinks I'm trying to do a comparison, which I'm not. I want to assign the Me._existingImports the value of Me.GetImportedAds(), but VB.NET complains about DataTable not having a = operator.
How can I force it to use the assignment operator instead of the equality operator?
In C# this works perfectly fine:
() => this.existingImports = this.GetImportedAds()
For now the solution will be to use a standalone method, but that's way more code than needed.
When using Function(), you really define an anonymous function which means you map values to values.
Therefore Function() strictly needs an expression (like x or 42 ...) as the body, which an assignment is not! (Assignments don't evaluate to values like in C-style languages in VB)
Thus what you need is not a Function() but a Sub(), which contains statements (actions) rather than values.
Sub() Me._existingImports = Me.GetImportedAds()
C# doesn't distinguish here, the (much nicer) ... => ... syntax covers it all.
I am using Linq to convert an array of any object to a CSV list:
String.Join(",", (From item In objectArray Select item.ToString()).ToArray())
This is giving me the strange error: "Range variable name cannot match the name of a member of the 'Object' class."
I can get round it by wrapping the string in a VB StrConv method, with a setting of "Nothing":
String.Join(",", (From item In oArray Select StrConv(item.ToString(), VbStrConv.None)).ToArray())
However, this seems like a bit of a hack and I would like to avoid it.
Does anyone have any ideas when this problems occurs, and any better ways to get round it?
Modify your code to:
String.Join(",", (From item In objectArray Select stringVal = item.ToString()).ToArray())
The problem is VB gives a name to the variable returned by Select clause. Implicitly, it tries to give the name ToString to item.ToString() which will clash with ToString method. To prevent so, you should explicitly specify a name (stringVal in above line).