Get The Count From A Query Expression In Linq?

Jan 25, 2012

What I have right now is this:

Dim users = From users In tempTable _
Distinct Select users.Item("s_userid")
Dim usersCount As Integer = users.Count

But I pretty sure I shouldn't have to do that. I should be able to get the count in/from that first linq query.

View 2 Replies


ADVERTISEMENT

LINQ Statement Where Result Count Is Used In Expression's Condition?

Mar 23, 2009

I have a requirement where I have to add items into a List(Of T) (let's call it Target) from an IEnumerable(Of T) (let's call it Source) using Target.AddRange() in VB.NET.

Target.AddRange(Source.TakeWhie(Function(X, Index) ?))

The ? part is a tricky condition that is something like: As long as the as yet unenumerated count is not equal to what is needed to fill the list to the minimum required then randomly decide if the current item should be taken, otherwise take the item.Somethig like...

Source.Count() - Index = _minimum_required - _curr_count_of_items_taken _
OrElse GetRandomNumberBetween1And100() <= _probability_this_item_is_taken
' _minimum_required and _probability_this_item_is_taken are constants

The confounding part is that _curr_count_of_items_taken needs to be incremented each time the TakeWhile statement is satisfied. How would I go about doing that? I'm also open to a solution that uses any other LINQ methods (Aggregate, Where, etc.) instead of TakeWhile.If all else fails then I will go back to using a good old for-loop =)

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

Convert LINQ Query Expression From C# To VB?

Mar 10, 2009

I have the following C# LINQ query expression:

[code]...

how to convert this to VB.NET?

View 1 Replies

C# - What Operators Should Have LINQ Query Expression Support

Oct 2, 2009

Of the 51 Standard Query Operators (of which only 42 are actually query operators), only 24 are directly supported by Visual Basic 9 and just 11 by C# 3: Query Expression Syntax for Standard Query Operators.In many cases, query syntax is arguably more readable than the equivalent method syntax, especially when transparent identifiers are involved. However, that readability breaks down if you have to combine queries and method calls.So the question: What query operators, current or hypothetical, would you like to have your language of choice support in query expression syntax?

View 2 Replies

Linq Expression Chain Syntax For In Query?

Feb 25, 2011

I have a query that I cannot seem to replicate in expression method chain syntax. I have two tables "User" and "UserPayment". User and UserPayment have a one to many relation i.e. One User can have many UserPayments.

View 1 Replies

C# - LINQ To SQL To Query Parent And Count Childs In Hierarchy?

Dec 8, 2010

I'd take both C# and VB.NET suggestion.I'm using LINQ to query data. I'm trying to query the parent and count the child tags.Here's my Tags table column:

TagId (int primary)
TagName
ParentId (int Allow NULL referred to TagId column)

Here's some sample data:

[Code]...

View 3 Replies

LINQ - Query Children Object If Parent Count Is = 1

Apr 17, 2012

I have a the following Class structure. Company > List(of Departments) > List(of Employees) I want to Query a Company to find out if it has a department of the following name and a Employee in that department with the following ID! How could I query this. The way I have the code it expect to return one department so I have it doing

[Code]...

View 2 Replies

Sql - Group By Having And Count As LINQ Query With Multiply Nested Tables?

Feb 28, 2012

I have the following SQL query to return all Customers who have no OrderLines with no Parts assigned - i.e. I only want the customers within which every order line of every order has no parts assigned - (in the actual problem I am dealing with a different domain but have translated to customers/orders to illustrate the problem)

SELECT c.Customer_PK
FROM Customers c
INNER JOIN Orders o[code].....

This works but generates less than optimal SQL - it is doing a subquery for Count on each row of the customers query rather than using Group By and Having. I tried making the LINQ Group By syntax work but it kept putting the filter as a WHERE not a HAVING clause.Edit in response to Answers below: I am accepting JamieSee's answer as it addresses the stated problem, even though it does not produce the GROUP BY HAVING query I originally had.I am a VB developer so I have had a crack translating your code to VB, this is the closest I got to but it does not quite produce the desired output:

Dim qry = From c In context.Customers
Group Join o In context.Orders On c.Customer_PK Equals o.Customer_FK
Into joinedOrders = Group[cod].....

The problem I had is that I have to push "jl" into the Group By "Key" so I can reference it from the Where clause, otherwise the compiler cannot see that variable or any of the other variables appearing before the Group By clause.With the filter as specified I get all customers where at least one order has lines with no parts rather than only customers with no parts in any order.

View 3 Replies

ASP.NET MVC - Linq Query With Count Returns Anonymous Type - How To Display In View

Sep 29, 2011

So I'm writing a query as follows:

Dim assSummary = From a In db.Assignments
Join ur In db.UserRegions
On a.Origin.ID Equals ur.Region.ID

[code]....

In the controller I can return the data easily as follows:

For Each c In assSummary
MsgBox(c.Description & " " & c.AssCount)
Next

If I pass the object through to the view using Viewdata("assSummary") = assSummary, how do I display the data? Every method I've tried results in messages about 'VB$AnonymousType_7(Of Integer,String) and I don't know how to retrieve the data from the anonymous type.

View 1 Replies

DataColumn.Expression Count - Filter On The Day, Count The Rows And Then Populate This Added Column With The Result?

Nov 2, 2010

I have added a column to a Datatable called CallsPerDay which is there to tell me how many telephone calls have been made on a particular day or days.Is there a datacolumn.expression which will allow me to Filter on the day, count the rows and then populate this added column with the result.

View 1 Replies

LINQ Query Is Enumerated - Turn On Some Kind Of Flag That Alerts Me Each Time A LINQ Query Is Enumerated?

Sep 22, 2009

I know that LINQ queries are deferred and only executed when the query is enumerated, but I'm having trouble figuring out exactly when that happens.Certainly in a For Each loop, the query would be enumerated.What's the rule of thumb to follow? I don't want to accidentally enumerate over my query twice if it's a huge result.

For example, does System.Linq.Enumerable.First enumerate over the whole query? I ask for performance reasons. I want to pass a LINQ result set to an ASP.NET MVC view, and I also want to pass the First element separately. Enumerating over the results twice would be painful.It would be great to turn on some kind of flag that alerts me each time a LINQ query is enumerated. That way I could catch scenarios when I accidentally enumerate twice.

View 3 Replies

.net - ActiveRecord Linq/NHibernate Linq Not Building Query Completely?

Jul 14, 2011

given this function:

Public Function Search(ByVal StartIndex As Integer, _
ByVal MaxResults As Integer, _
ByVal AccountNumber As String, _
ByVal LastName As String, _

[Code]....

instead of what I expected:

Select count(*) from remitline where lastname = "smith"

What am I doing wrong building up the where clause? I'm using Castle ActiveRecord 2.1

View 1 Replies

Performance Gain In LINQ Query Vs LINQ Stored Procedure?

Aug 6, 2009

if there is that much of a performance gain in running a LINQ stored procedure versus a LINQ query?

View 1 Replies

Asp.net - Complex Linq Query - Add A .Where() Or .Any() Predicate To Reduce Query Complexity (in VB)?

Sep 14, 2010

I have to join two main tables, and I need to filter the results by elements in an ASP.NET web form. These filters are created on the fly so I have to use a lot of where extensions to filter the query. I want to execute the query with as optimized SQL as possible.

I am first doing a simple join between TW_Sites and TW_Investigators. Then there are two sub-tables that are involved. TW_InvestigatorToArea and TW_InvestigatorToDisease. While most of the where clauses are working fine, I have found a performance issue that won't be an issue right now, but will be an issue as the table gets bigger.

The arrays DiseaseCategories and DiseaseAreas would be the results of a CheckBoxList result.

Protected Sub LoadResults()
'Get Dictionary of Filters
Dim FilterDictionary As OrderedDictionary = Session.Item("InvestigatorFilterDictionary")
' Initialize LinqToSql

[code]....

View 2 Replies

VS 2008 Use Regular Expression To Count Occurrence Of Purpose

Dec 1, 2011

I need to count the number of:

[Code]...

Can Regular Expression be used of this purpose? It's okay if they're in separate function, but I need to somehow get the counts of each of the above.

View 1 Replies

LINQ Query Using The Dynamic LINQ Library?

Mar 31, 2011

Forgive my ignorance on this.I have this LINQ Query:Dim ngBikersDataContext As New CarBikeWalkDataContext

bikersList = (From c In ngBikersDataContext.Reg_Bikers _
Order By c.L_Name _
Select New Bikers() With { _
.BikerID = c.BikerID, _
.F_Name = c.F_Name, _

[Code]...

with the error "Overload resolution failed because no accesible 'Select' accepts this number of arguments."
Over the "NEW" I get an error " ')'expected."

View 1 Replies

Linq Expression To .net?

Nov 17, 2009

convert this C# code to VB, tried couple of converters and they dont work properly.

[Code]...

View 6 Replies

Convert Linq.expression From C# To VB?

Mar 30, 2010

[QueryInterceptor("Somethings")]
public Expression<Func<Something, bool>> OnSomethings()
{
// Code here
}

I had a view guesses, looked on msdn, but there are no examples matching the way that that is used.

View 1 Replies

Converting LINQ Expression From C#

Nov 18, 2010

I'm having a little trouble converting some LINQ to VB.[code]Online code translators are not helping, and my unfamiliarly with VB LINQ is not very good.

View 2 Replies

VS 2010 Use Take() With Linq Expression?

Aug 6, 2011

THE CODE IS :

var query1 = ((from c in dt.AsEnumerable()
orderby c.Field<int>("1") descending
select c));
var query2 = (from c in query1.Take(10)
select c);

[Code]...

View 1 Replies

.NET XML Literal Expression Expected With Linq To XML?

Jun 9, 2010

I have the same problem as stated in this question, but the accepted solution there was a "works on my machine" answer.

[Code]...

And I receive the error: BC30201: Expression expected.

Does anyone have a more detailed idea of what could cause this?

View 1 Replies

C# - Join The Objects In A LINQ Expression?

Dec 15, 2011

How to Join the objects in a LINQ select in this sample (C# variants accepted as well):

Class Room
Public Area As Integer
End Class
Class RoomPair

[code]....

View 4 Replies

Dynamic Linq Expression Tree After Where

Oct 28, 2011

1) I am begginner to dynamic Linq and having serious trouble creating expression tree after WHERE

say.: items.Category_ID=4

I tried to construct it like: Dim products = From items In mydatacontent.Products

Dim AA As ParameterExpression = Expression.Parameter(GetType(String), "items")

Dim left1 As Expression = Expression.Property(AA, GetType(String).GetProperty("Category_ID")) here is the error Dim right1 As Expression = Expression.Constant("4") Dim BB As Expression = Expression.Equal(left1, right1)

[Code]...

View 2 Replies

Linq - .NET Logical Expression Evaluator?

May 21, 2010

I need to test a logical expression held in a string to see if it evaluate to TRUE or FALSE.(the strig is built dynamically)For example the resulting string may contain "'dog'<'cat' OR (1>4 AND 4<6)". There are no variables in the string, it will logically evaluate. It will only contain simple operators = > < >< >= <= and AND , OR and Open and Close Brackets, string constants and numbers. (converted to correct syntax && || etc.)

I currently acheive this by creating a jscipt function and compiling it into a .dll. I then reference the .dll in my VB.NET project.

class ExpressionEvaluator
{
function Evaluate(Expression : String)
{
return eval(Expression);
}
}

Is there a simpler method using built in .NET functions or Lamdba expressions.

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

LINQ Expression Starting With From And Aggregate

May 4, 2012

[code]The LINQ query going into "result" is incomplete because I'm stumped at what is happening there. I expect FirstOrDefault to refer to refer to a single Child object at that point in the expression, but it refers to a collection of child objects. Why? What is the best way to get a list of parents that have no associated child or have a child fitting a particular condition? (My actual code will have one child max, so this sample code is not representative.)I just don't understand what the collection of FirstOrDefault could be referring to. It should really be just 1 value or nothing at all times.

View 1 Replies

VS 2008 Translate C# Linq Expression?

Nov 28, 2010

translation of the following C# Linq Expression (in VB.NET 2008):

syncContext.Send((obj) => eventToBeFired(this, new EventArgs()), null);

Is used in this subroutine:

private void FireEvent(EventHandler eventToBeFired)
{
if (eventToBeFired != null)
{

[code]....

View 3 Replies

"System.ArgumentException: An Item With The Same Key Has Already Been Added." When Trying To Order An In-memory LINQ Query By A LINQ Association?

Jan 27, 2011

We are doing a query against an in-memory collection of LINQ data objects. The wrinkle is that we are ordering by a column in a related table whose records have not necessarily been loaded yet (deferred loading:)

Dim oPkgProducts = _
From b In oBillPkg.BillProducts _
Where b.Successful.GetValueOrDefault(Common.X_INDETERMINATE) = _

[code]....

View 1 Replies

LINQ To SQL - Add In Row_Number To A LINQ To SQL Query?

Aug 31, 2011

How do I add ROW_NUMBER to a LINQ query or Entity? How can I convert this solution to VB.NET?

[Code]...

I'm having trouble porting that last line. I have been unable to locate a VB.NET example. I'm actually not looking for any paging functionality like the example provides, just good old-fashioned Row_Number(Order By X) row index.

View 1 Replies







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