Set An Indexing Variable In Linq/Lambda Expression?
Apr 16, 2010
I have a lambda expression that builds a list ofanonymous types. I would like to include a property in the anonymous type that is a counter. So the first entry is 1, second 2, third 3, and so on.I thought I recallenyone know the syntax for defining an indexing variable as part of a Linq query or lambda expression?
Where can I find complex LINQ examples made using VB.NET Lambda Expression syntax?During my searches I always found 101 LINQ Samples but they use the other notation and for me is not always clear how to transform that code into a lambda expression.
I am trying to convert the following code for the variance calculation public static double Variance(this IEnumerable<double> source){ double avg = source.Average(); double d = source.Aggregate(0.0, (total, next) => total += Math.Pow(next - avg, 2)); return d / (source.Count() - 1); } Described on CodeProject into corresponded VB.NET lambda expression syntax, but I am stuck in the conversion of Aggregate function. How can I implement that code in VB.NET?
Can a LINQ or a LAMBDA expression be used to count the 1's in a binary string ?For example, if I convert a number to its BINARY using.>>
Dim binaryString As String = Convert.ToString( numberVariable, 2 )
1) Without using a loop such as a FOR NEXT loop, can I count the number of 1's in the STRING using LINQ or a LAMBDA expression?
2) Can I get the indexes of where the 1's are at in a collection such as a LIST using LINQ or a LAMBDA expression?I know the syntax of some LINQ expressions, however, I don't know which method may be suitable.
Edit: Here is a much more simpler example of this issue (i've deleted my original question):
Dim numbers1 As New List(Of Int32)({1, 2, 3}) Dim numbers2 As New List(Of Int32)({3, 4, 5}) For Each n1 In numbers1 ' no warning ' Dim contains = numbers2.Contains(n1) Next
[Code]...
The local variable number1 is redundant and makes the code less readable as @Meta-Knight already has emphasized. Note: All three ways are safe and give the correct result.
Possible Duplicate:Why is it bad to use a iteration variable in a lambda expression.why do I get: "iteration variable in a lambda expression may have unexpected results"? Suppose I have the following code:
GenerateTheList is function.Need help Private Sub buttGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttGenerate.Click Dim thread1 As New Thread(New ThreadStart(GenerateTheList)) thread1.Start() End Sub
If I put New Action(AddressOf PrivateMesage), then I get the following error: Bounds can be specified only for the top-level array when initializing an array of arrays. on the following code
I am dumb founded at this statement....maybe its just too many hours/days of doing C# to VB.Net conversion but i am drawing a blank on this conversion.
In looking at Fuzzy Date Time Picker Control in C# .NET? Piotr Czaapla's answer to that question is exactly what I need. unfortunately, I'm a VB.NET guy and I'm not that familiar with lambda expresions, so my attempts to convert the code have resulted in hours of misplaced parenthesis and banging my head with a brick.
Any chance some bi-lingual wizard could convert the C# code to VB.net for me?
I will probably sound like a bad developer here, but I am attempting to convert some code from the following page from C# to VB.NET:The code I am having trouble converting is from Step 4 of "Joining a Multicast Group" on the page, and here is the code with the comment lines removed:
private void Join() { _receiveBuffer = new byte[MAX_MESSAGE_SIZE];
Public Function GetFunc(Of TSource, TResult) _ selector As Expression(Of Func(Of TSource, TResult)) _ As Func(Of TSource, TResult)
[code]....
UPDATE:I have the following issue, it's a part of a function:
Dim obj As Object = value For Each exp In expressions If obj Is Nothing Then Return [default]
[code]....
Exception Details:
ArgumentException:Object of type 'System.Linq.Expressions.Expression`1 [System.Func`3[System.Char,System.Char,System.Char]]'cannot be converted to type 'System.Func`3[System.Char,System.Char,System.Char]'.
If been try to convert some code I have from VB.net to C# but I just cannot get it working!first is the vb code I'm only interested in the line that has the Lambda Expression for the C#[code]......
I know in VB Lambda expressions must return a value and can only have one statement. But in my case I just need to execute a Sub that does not return a value. Is there a way to trick it by somehow returning a dummy value yet still call my Sub? If possible what would the syntax be like?
I am having an issue using the vb equivilant of the MAX lambda expression. at foos.Max(function(x) x.id) when I try to intellisense the property ID VS will not show it. But when i run the example it works. Is there something I am doing which is wrong, an i am just lucky it runs? [Code]
Private Sub InsertItemInCache(Of T)(ByVal item As CachedItem(Of T), ByVal dependency As AggregateCacheDependency, _ ByVal key As String, ByVal updateCallBack As CacheItemUpdateCallback)
I am Creating a Lambda Expression, but it' not getting me the write Out put :
Public Class Form1 Delegate Function GetLastChar(ByVal S As String) As String Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
[code]....
I want Send a Name like "Arindam" and its return me "m", the last character.but in messagbox return value shows "fasle" I can't under stand why it's showing "False" instead of "m", the last character.
I am having a generic ListOf-collection holding the self-created User-object, I can filter for users by using lambda-expression like this: Dim reviewers As List(Of User) = users.Where(Function(u) u.DvRoleID = 2).ToList
Instead of using the lambda expression I can also write the function and call it as argument like this: Public Function GetReviewer1(ByVal u As User) As Boolean If u.DvRoleID = 2 Then Return True [Code] .....
Now the question: why can't I create a delegate having the same signature as the GetReviewer1 function and use the delegate as argument inside the where-clause like this: Delegate Function Criteria(ByVal u As User) As Boolean Public Function GetReviewer1(ByVal u As User) As Boolean If u.DvRoleID = 2 Then [Code] ..... The compiler does not accept the crit delegate as argument.
I am trying to see if the controller class name is the name of the controller name passed in. I think my lambda expression is wrong and that is why it isn't working properly. I am passing in the correct controller names, but the export variable is always null.
Public Function CreateController(requestContext As System.Web.Routing.RequestContext, controllerName As String)
In MSDN documentation defination about lambda expression is "A lambda expression is a function or subroutine without a name that can be used wherever a delegate is valid ". Here validity of delegate is necessery only when we are passing lambda expression as an argument to a method.
Should I be concerned about the performance of the Compile method here?[code]...
The reason I want the lambda expression is because I want that function to be able to get all the way through LINQ-to-SQL to be converted to SQL. The reason I want the direct version is because I have other code that I want to validate individual serial numbers before submitting changes. And I fear that GetValidSerialNumbers.Contains(serialNumber) will execute a more-complex-than-necesssary query.
With ASP.NET MVC 1.0, .NET 3.5 and C# you can easily pass a method a lambda expression that will do a "Response.Write" some content when it's executed within the method[code]...