C# - Select` And `sub` Have Brackets Around Them In VB Linq Expressions?
Jul 12, 2011
I converted the following query from C#:
src.Select((c, i) => src.Substring(i)).Count(sub => sub.StartsWith(target))
To the VB.NET query:
src.[Select](Function(c, i) src.Substring(i)).Count(Function([sub]) [sub].StartsWith(target))
Using Developer Fusion. I was just wondering why the VB.NET version has [] throughout.
View 3 Replies
ADVERTISEMENT
Feb 28, 2012
The pattern I'm trying to avoid is checking if a string (normally a control's text value) is null/empty, and if it is, comparing it using Contains to a field in my data. Obviously the field isn't hard-coded into my extension, neither is the object type.What I've got works perfectly in Linq to Objects, but I get the generic run-time error "LINQ to Entities does not recognize the method 'System.String Invoke(GenericQueryHelper.Customer)' method, and this method cannot be translated into a store expression." when using an entity framework model.
Here's what I have:
<System.Runtime.CompilerServices.Extension()>
Public Function CompareAndFilter(Of T)(source As System.Linq.IQueryable(Of T), expressionField As System.Linq.Expressions.Expression(Of System.Func(Of T, String)), compareTo As String)
[code]....
I want my usage to look something like this:
Dim results = repository.Customers.CompareAndFilter(Function(c) c.FirstName, searchText)
I do need to get this running against a SQL database really, as it is filtering results, so I don't want to be doing that in memory.
View 2 Replies
May 3, 2011
I'm trying to implement multicolumn filtering using LINQ expressions in a class that extends BindingList(Of T). Here is the relevant code:
Public Function GetFilterPredicate() As Func(Of T, Boolean)
Dim expressionList As List(Of Expression) = New List(Of Expression)
For Each item as FilterInfo in _FilterList
[code]....
However, an exception is thrown at the Expression.Call statement. I can't quite figure out the right arguments to supply. As it is now, I am getting this error when I run the code:
InvalidOperationException was unhandled:No generic method 'Equal' on type 'System.Linq.Expressions.Expression' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic.
View 1 Replies
Jan 5, 2011
I want to create a simple function that does the following:
Sub SetValue(Of TInstance As Class, TProperty)(
ByVal instance As TInstance,
ByVal [property] As Expression(Of Func(Of TInstance, TProperty)),
[code].....
View 1 Replies
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
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
Jan 30, 2010
how to create a lamdba expression instead of using a regular comprehension linq queries. All of the examples i see are not query a database using linq to sql. i want to able to construct a lambda expression that get my the orders from the northwind database where the country equals "us". I know how to construct this using the standard linq query, but just not using lambdas in vb.net.
View 1 Replies
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
Nov 24, 2010
I have a list of properties and values that i'd like to use to dynamically build an Expression(Of Func(Of MyClass,MyClass))
I can run through the list and create each Expression by itself, but the only way I know how to combine them would be to use Expression.And or Expression.AndAlso, but that returns a BinaryExpression rather than my original Expression(Of Func(Of MyClass,MyClass)).
View 1 Replies
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
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
Apr 16, 2009
I'm experimenting with linq compount selects. I find the following query not to return any element:
Dim q = From s In d.GetChildRows("DossierSinistri") _
From i In s.GetChildRows("DossierIncarichi") _
Select s
[Code].....
View 2 Replies
Feb 3, 2011
Why can't I get any values back when I use Linq to Sql? BHS_TimeSheet is my database table in which have some records. Model.TimeSheet is a class I create in the model. Private db As DataFactoryDataContext
[Code]...
View 1 Replies
Nov 15, 2011
Today I ran into an issue with LINQ to objects (not SQL) that popped up due to a typo. I had a .Select one place and a .Where in another place. I was expecting same result but they are showing different numbers. Assume somelist has 10 elements with all elements having qty = 0
[Code]...
View 4 Replies
Mar 16, 2011
When using VB.NET I can do the following:
Public Function GetUser(ByVal ID as Integer) As User
Dim dc As New YesEntities()
Return (From u in dc.Users Where u.ID = ID).Single()
End Function
And on my .aspx/.vbhtml page I can access the user's department name like this:
Dim DepartmentName as String = new User().GetUser(12).Department.Name
[Code]...
View 1 Replies
Jul 25, 2010
i try to select controls using LINQ but it show error
here is the code:
Dim testcontrol As VariantType
testcontrol = From cControl As Control In Me.Controls.OfType(PictureBox) Select cControl Order By Name Ascending
[Code].....
View 2 Replies
Jan 9, 2009
I have seen different examples in Linq and one used Select and another didnt but both return the same results. Is there a reason why we should use Select ?
[Code]...
View 1 Replies
Mar 18, 2010
I can't quite figure out why this Linq Statement isn't working as i would expect:
[Code]....
I would assume that this would create a new collection of anonymous types, that would be distinct. Instead it creates a collection the size of the "ThisParentCollection" with duplicate "MyAnonymousType" in it (duplicate id's).
View 1 Replies
May 28, 2009
So say I have a collection of Bloops
Class Bloop
Public FirstName
Public LastName
[Code]....
Is it possible using Linq to select the FirstName and LastName out of all the Bloops in the collection of Bloops and return a collection of Razzies? Or am i limited to a For-Loop to do my work?
To clear up any confusion, either VB or C# will do. Also this will probably lead to me asking the question of (What about using a "Where" clause).
View 4 Replies
Aug 20, 2010
I have a single columned datatable inside a single tabled dataset.I just want to convert this dataset to distinct rows. Here is my code, it gives compile error '.' expected.
Dim query = _
From email In ds.Tables(0) _
Select email.Field<string>("Email").Distinct()
EDIT: I changed to (Of String) and it works... BUT NOW 'query' is an ienumerable collection of characters... not a datatable... so how do I convert back easily without manually doing a loop?
View 1 Replies
Jun 21, 2010
Linq to SQL query to Select the maximum "MeanWindSpeed" value? The database name is "WeatherArchives" The TableName is "TblValues" The Column is "MeanWindSpeed" And Also, I would like to have a query to get me the Max "MeanWindSpeed" in each year,
View 1 Replies
Jan 4, 2012
I've a collection with the data like this.[code]...
how can select the distinct data using linq?
View 2 Replies
Nov 2, 2009
Dim MyQuery = From c In xdoc.Descendants() _
Where c.Attribute(OriginY) IsNot Nothing _
Order By Val(c.Attribute(OriginY).Value), Val(c.Attribute(OriginX).Value) _
Select c.Attribute(UniStr)
Right above you can see my First! linq attempt! And here comes my first question.
How can i select more than one column in a linq query in vb.net?
For example... Select c.Attribute(UniStr) AND c.Attribute(OriginY)
View 1 Replies
May 17, 2009
I need to have a query which list all the user according to the date value in the database. Problems is this, in the database the date format is 5/5/2009 4:30:12 but I want to compare with 5/5/2009. I think the value of the based date is 5/5/2009 12:00:00 and that's why I couldn't query it.
The query is something like
dim db = new databcontext
dim user = from u in db.datacontext where u.signUpTime = 5/5/2009 select u.
View 1 Replies
Aug 23, 2010
Dim orders = From tt In testTable _
Order By tt.Item("OrderNumber") _
Select tt.Item("OrderNumber"), tt.Item("OrderId")
This is breaking. Is there a way to do this? I would have thought it was easy enough. Obviously, I thought wrong....
View 1 Replies
Dec 14, 2009
I have this LINQ statement [code]...
Is there any way that I can use if/iif within select ?
View 1 Replies
Mar 28, 2012
I'm pretty new in Linq and the first problem I've is to select the Distinct values from a ObservableCollection( Of T)
I've tried with[code]...
View 2 Replies
Mar 13, 2012
My LINQ query:
Dim groupedData = (From p In pData _
Group By p.TruncParam Into Group) _
.SelectMany(Function(g) g.Group) _
.Select(Function(d, idx) New With { _
[Code]...
When the TruncParam changes, I would like the index (idx) in the Select clause to reset to 1. So, in the list above, the Index should be SOLUB0001, SOLUB0002, INSOL0001, INSOL0002...CLRES0001, SUMCA0001, SUME0001.
How should I alter the LINQ query?
View 1 Replies
Dec 1, 2009
[Code]...
I am trying to replace tasks.IndexOf(t) + 1 with something a little simpler. Is there any built in functionality for this? Hrmm xml literals do not seem to translate well on here....
View 3 Replies
Mar 12, 2012
I was wondering if there is a neat way do to this, that DOESN'T use any kind of while loop or similar, preferably that would run against Linq to Entities as a single SQL round-trip, and also against Linq To Objects. I have an entity - Forum - that has a parent-child relationship going on. That is, a Forum may (or in the case of the top level, may not) have a ParentForum, and may have many ChildForums. A Forum then contains many Posts.
What I'm after here is a way to get all the Posts from a tree of Forums - i.e. the Forum in question, and all it's children, grandchildren etc. I don't know in advance how many sub-levels the Forum in question may have. (Note - I know this example isn't necessarily a valuble use case, but the Forum object model one is one that is familar to most people, and so serves as a generic and accessible premise rather than my actual domain model.)
View 2 Replies