Linq - Write Lambda Select Method In .net?

May 22, 2012

For I've tried this:Dim exampleItems As Dictionary(Of String, String) = New Dictionary(Of String, String)
Dim blah = exampleItems.Select (Function(x) New (x.Key, x.Value)).ToList 'error here

But I'm getting a syntax error and all the examples that I've seen are in C#.

View 1 Replies


ADVERTISEMENT

Nested Multi-line Lambda Function As Parameter For LINQ Select Is Causing An Error

Jun 29, 2010

I'm trying to use a nested multi-line lambda Function in VB.NET and am getting an error. Here's what my code looks like:

cartItems = cartItems.Select(Function(ci) New With {.CartItem = ci, .Discount = discountItems.FirstOrDefault(Function(di) di.SKU = ci.SKU)})
.Select(Function(k)
If k.Discount Is Not Nothing Then

[code]....

View 2 Replies

Translate A Call Of The LINQ Extension Method Select Into A Corresponding Expression Tree?

May 23, 2012

The following LINQ query resp. call of the extension method Select in Visual Basic 2010 is working fine:

Dim qAvSalary = qJobData.Select(Function(e) e.AvSalary) But doing so I am not able to specify the name of property I want the query (e.g. AvSalary) using a string variable. This should be possible if I use a LINQ expression tree. Searching and trying a long time on how to translate the query to a corresponding expression tree was not successful. My final approach is:

[Code]...

View 1 Replies

Why Does Linq Lambda Work One Way But Not The Other

Dec 7, 2011

I am learning MVC3 and transitioning to VB.NET from C# at the same time (Fun, I know). I am running through the MvcMusicStore sample for learning MVC3. All the samples are in C# so I am converting them to VB. I came across one piece of code that I was unable to directly convert and had to change slightly to get it to work.

why one way works and the other doesn't.

This does not work:

Dim albums = New List(Of Album)() From { _
New Album() With { _
.Title = "A Copland Celebration, Vol. I", _

[Code].....

View 2 Replies

Breakpoint Not Hit When Method Called In Lambda

Aug 2, 2011

When I put a breakpoint in a method I'm calling from a lambda expression, the breakpoint is never hit. When I move the method call outside the lambda, the breakpoint hits.

For example:

Function IncrementAll(ByVal items As IEnumerable(Of Integer)) As IEnumerable(Of Integer)
Return items.Select(Function(i) Increment(i))
End Function

[Code]....

If I call IncrementAll, the breakpoint in Increment does not get hit. Is there a way to make VS 2008 stop on these breakpoints? I hate the thought of rewriting all my LINQ into loops just for debugging.

View 1 Replies

Linq Doing A Group In A Lambda Subselect

Feb 23, 2012

I have a user input screen that allows them to select input values and the resulting query is dependent on which values the user entered. The resulting query groups up the results to present a list of unique customers

Simplified table design - A customer can be in many states
[code...]

View 1 Replies

Linq - Entity Framework Many-to-many Using Lambda

Feb 11, 2010

I'm using Entity Framework in Visual Studio 2010 Beta 2 (.NET framework 4.0 Beta 2). I have created an entity framework .edmx model from my database and I have a handful of many-to-many relationships.

A trivial example of my database schema is

Roles (ID, Name, Active)
Members (ID, DateOfBirth, DateCreated)
RoleMembership(RoleID, MemberID)

[Code]....

For brevity I turned the list of all the columns from the Members table into *

As you can see it's just ignoring the "Role" query.

View 2 Replies

Linq - Examples Of .NET Lambda Expression?

Feb 3, 2011

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.

View 1 Replies

C# - Convert This Anonymous Method / Lambda Expression Across?

Feb 23, 2010

How would you convert this to VB (using .NET 4.0 / VS2010) ?

bw.DoWork += (o, args) =>
{
Code Here
};

I thought maybe like this:

AddHandler bw.DoWork,
Function(o, args)[code]....

But it says Function does not return a value on all code paths.

View 6 Replies

C# - Use A Method Instead Of Lambda Expression With Extra Parameters?

Jan 27, 2012

I have an inline lambda expression that I would like to use throughout my application. I just can't seem to find a reference on how to do this with more parameters than the element being tested. Here is a quick example of what I currently have.

Private Sub Test()
Dim List As New List(Of String) From {"Joe", "Ken", "Bob", "John"}
Dim Search As String = "*Jo*"

[Code].....

View 4 Replies

Declaring An Anonymous Method With .net Action(Of T) And Lambda?

Apr 22, 2009

Imports System.Reflection
Public Class Test
Private Field As String
End Class
Module Module1
Sub Main()
Dim field = GetType(Test).GetField("Field", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance)

[Code]...

View 2 Replies

LINQ - Calculate Variance With Lambda Expression

Feb 3, 2011

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?

View 1 Replies

Loading Record By Predicate / Lambda Via LINQ

Jan 12, 2011

I am pretty confused about lambdas. What I am trying to do here is write a function that will return an object from a certain table with a certain criteria. So lets say I can write:
function GetRecord(TableName as string,Criteria as string) as object
'do the linq-stuff
end function

Now I don't care if the parameters are strings or lambdas or whatever, but the end result must be that at runtime I don't know which table and which criteria will be used. As sometimes I need to get a customer record by email and sometimes a product by id etc. If possible I would prefer returning a list of matching objects and then I would just use .firstordefault when I want 1 (such as by id...).

View 2 Replies

Pass A Lambda Function To Linq's Orderby?

Jan 24, 2010

I am trying to pass a lambda function to Linq's orderby and cannot get it to work.

The relevant code is:

Dim myordering = Function(m) m.Count
If isAlphaOrder Then
myOrdering = Function(m) m.Key 'Order by Alphabet

[Code].....

View 4 Replies

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?

View 10 Replies

Method - Action(Of T) And Lambda - Error: 'Expression Does Not Procedure A Value'

Apr 22, 2009

Imports System.Reflection

Public Class Test

[CODE]......................

'This line indicates a compile error: 'Expression does not procedure a value':

[CODE]..................

View 8 Replies

Linq - Order By Multiple Columns Using Lambda Expressions

Nov 16, 2009

I'm still trying to get my head around the whole "Lambda Expressions" thing.

Can anyone here give me an example ordering by multiple columns using VB.Net and Linq-to-SQL using a lambda expression?

Here is my existing code, which returns an ordered list using a single-column to order the results:

Return _dbContext.WebCategories.OrderBy(Function(c As WebCategory) c.DisplayOrder).ToList

Note: The WebCategory object has a child WebPage object (based on a foreign key). I'd like to order by WebPage.DisplayOrder first, then by WebCategory.DisplayOrder.

I tried chaining the order bys, like below, and though it compiled and ran, it didn't seem to return the data in the order I wanted.

Return _dbContext.WebCategories.OrderBy(Function(c As WebCategory) c.DisplayOrder).OrderBy(Function(c As WebCategory) c.WebPage.DisplayOrder).ToList

View 2 Replies

VB Dynamic Run-Time Query With Linq And Lambda Expressions

Mar 16, 2009

I have an untyped dataset returned from my WebService. The user wants to dynamically construct a query referencing tables and columns and specifying values to test for.I have looked at Lambda Expressions and the Expressions.Expression Namespace. I think these provide the answer I'm looking for, but I'm not certain how to go about contructing the linq expressions to extract the datarows I'm looking for.

I plan on limiting the result to one datatable that the query will result in and i"ll provide the joins from the user's constructs.For a simple example I have a DataTable with (n) rows called "Table", and in it there is a computed column called "b_IsActive" that has the expression "Convert(IsActive, 'System.Boolean')". The user wants to retrieve all rows in "Table" where field "b_IsActive" is true.

View 5 Replies

Book Recommendation With Good Lambda Expressions And LINQ Chapters?

May 20, 2009

I'm looking for an advanced level VB.NET book which covers LINQ and Lambda Expressions.Generally I read C# .NET books due to lack of good VB.NET books when it comes to generic .NET Framework related subjects. However Lambda and LINQ is quite different in C# and VB.NET I'm looking for an advanced level VB.NET book on this subjec

View 1 Replies

Order A Collection Based On A Child Property Using LINQ Or A Lambda?

Jan 26, 2012

I get the following string expression provided:"ChildObject.FullName" ...where ChildObject is an instance property on the MyObject1 type.ChildObject has a property named "FullName" and I want to sort a collection of type "MyObject1" based on this child properties "FullName" value.

I can do this all day long on properties directly on MyObject1 but I run into 2 challanges when doing it on a child instance and I can't get all the pieces working. The main 2 challanges are:

MyObject1 has a few different child property types so I can't hardcode the type for ChildObject. The string could be of any type.The sort expression is a String and not a known type.Above the value returned from the last line in the expression (if I run the code outsode the OrderBy method, does provide me the 'FullName' information I need. So the code must be close, but it still does not work.

Any ideas on how I can accomplish this? What I am trying to prevent is hardcoding a series of 'If' blocks on the child's type to then hardcode in its type to the sort or OrderBy method.

View 2 Replies

Can A LINQ Or A LAMBDA Expression Be Used To Count The 1's In A Binary String Or Char Array

Sep 15, 2010

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.

View 2 Replies

Lambda Coding - Basic Code Of Lambda With Explanation

Jun 21, 2011

Im hearing about this, can someone explain it to me or please show some basic code of Lambda with explanation.

View 2 Replies

Using The Contains Method To Select The Appropriate Delivery Method

Mar 18, 2009

I'm trying to code an application that selects an appropriate delivery method from a list box. The user should be able to enter a part number consisting of numbers and either one or two letters. "MS" representing "Mail-Standard, "MP" for "MailPriority", "FS" for "FedEx-Standard", "FO" for "FedEx "Overnight" and "U" represents "UPS" I want to code it so that it does the above and displays an appropriate message when the part number contains an invalid letter or letters.

Option Explicit On
Option Strict On
Public Class MainForm

[Code]......

View 2 Replies

Lambda Tutorial And Solving A Lambda-Function

Dec 14, 2009

Is it possible to shorten the following function to a lambda expression?Or (to do the trick by myself) what is the best and most understandable for beginners tutorial for lambda in vb.net?[code]

View 2 Replies

Optimize Linq Contains Method

Apr 1, 2012

I need a list from OldGuids that are not in the NewGuids, so I used the Contains method, the problem is that it is already running more then half hour, is there a faster way? or how much longer would it take about? [code]

View 2 Replies

Linq To Object - Method Not Found?

Nov 11, 2010

I am trying to use a simple LINQ to Objects query.When I try to run my application I get the message:

System.InvalidOperationException was unhandled Message="An error occurred creating the form. See Exception.InnerException for details. The error is: Method not found: 'Void sharatTashlumimWS.paymentDetailsManager.set_ReleaseDate(System.DateTime)'."
Source="WindowsApplication1"
StackTrace:

[Code]...

View 2 Replies

Asp.net - LINQ To SQL - Select SUM With A WHERE?

Aug 17, 2009

I'm having trouble getting a Linq to Sql query to work. Basically, I want the sum of a column in a table for only rows matching a certain condition. A simplified version is this:

Orders = order that contains products
OrderProducts = relational table containing products that are in an order
Products = table containing information about products

I can get the total sum of the qty of products like this:

Dim totalQty = requests.Sum(Function(p) p.OrderProducts.Sum(Function(q) CType(q.ProductQty, Nullable(Of Integer))))

(Requests is a IQueryable of Orders)But I also need to get the sum of qty where the actual product meets a certain condition. Something like...

Dim totalQty = requests.Sum(Function(p) p.OrderProducts.Sum(Function(q) CType(q.ProductQty, Nullable(Of Integer))))... WHERE p.OrderProducts.Product.ProductAttribute = true

How would I go about getting that query with the additional where clause to only get the sum of the qtys where the productAttribute = true ?

View 1 Replies

Linq To SQL Sub Select?

Oct 12, 2009

I have the following query which groups some records and then filters where the count of the grouped records is 1.

I'd like to take the returned result and perform another query to retrieve the entire record from the JobcodesWorkingRollup table where the ParentNode column equals the result of this query:

Dim query = From r In context.GetTable(Of JobcodesWorkingRollup)() _
Group r By r.ParentNode Into g = Group _
Where g.Count = 1 _
Select New With {.cnt = g.Count, .nm = g.FirstOrDefault.ParentNode}

View 1 Replies

How To Write Linq Query For Xml In .net

Feb 3, 2010

I want to write a link query for my xml. Actually i dont know it.i have write some code here.

Dim query = _
From p In MyPermissionXml.Elements("menuNode").Descendants("menuNode") _
Where p.Attributes("title").ToString = "Company"

from where clause , i think it's wrong. how to represent an attribute here?

View 1 Replies

Write SQL Query In LINQ?

Oct 18, 2011

How can I write this SQL query in VB.NET LINQ?

[Code]....

View 1 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved