.net - Execute A LINQ-Query Immediately?

Jan 25, 2012

I don't like the verbose way of adding DataRows one by one via Tbl.Rows.Add, so i've created this pseudo LINQ query that does nothing but adding rows implicitely:

Private Function GetData() As DataTable
Dim years = {"2010/2009", "2009/2008", "2008/2007", "2007/2006", "2006/2005", "2005/2004", "2004/2003"}
Dim tbl = New DataTable
tbl.Columns.Add(New DataColumn("Year"))

[code]....

As you can see i don't need the result of the query at all, it's only purpose is to iterate and call DataTable.Rows.Add. So the result is already available in the DataTable itself and not needed from the query.Admittedly this question is somewhat hyphotetical because there would not be a big difference in using f.e. ToList and normally a query's purpose is to return something.But anyway, what's the cheapest way(in terms of memory consumption, execution time) to execute a LINQ query when only the execution and not the result matters?Edit: Ok, thisquestion was a quick-shot and a chicken-and-egg problem. I wanted to reduce 20 lines of code to a single line but noticed that i need some kind of DataSource for the LINQ-Query. Therefor i've created the Array. But at this point i could have simply used a for-each loop as well to add the DataRows.

Conclusion: Use a LINQ-Query for what it's for: a query. Then this question is pointless since there is no cheapest way but only that which returns the correct result.

View 2 Replies


ADVERTISEMENT

Have A LINQ Query Execute In A BackgroundWorker?

Jan 22, 2010

I would like to run a query in background (using a BackgroundWorker) in this way[code]...

...but, because of deferred execution, the actual getting of the data does not happen in the "BackgroundWorker" thread, but in the "RunWorkerCompleted" Sub, making the BackgroundWorker itself useless.Is there a right way to do this without looping through the "MyQuery" to get the results and copying them to another object to pass it to the BindingSource?

View 2 Replies

LINQ Compiled Query Won't Execute

Feb 7, 2011

I'm testing the Compiled Queries for the first time.[code]...

View 3 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

Parameterized Query : Works With Execute Query But Not In Code?

Oct 22, 2009

I have created a parameterized query in my tableadapter. Here is the SQL:

SELECT CalcinerID, Comments, CorrectiveActions, CrucibleOxideWt, CrucibleWeight, DCPressure, DCTemperature,
Date, DischargePressure, FeedPressure, FiredCrucibleOxideWt, GmSampleWt, LIMSNumber, LotNumber, MaterialType,
OperatorID, QualLabOperator, QualLabTimeStamp, RecordID, ToteBinNumber [code]....

When I use the Execute Query button in the Tableadapter Query Builder, the query works fine; I get all the rows I expect. However, when I run this in my code, the query behaves like an equality statement (i.e. Lotnumber = @Lotnumber) rather than a LIKE statement (Lotnumber LIKE '%' + @Lotnumber + '%').Here is the code that is not behaving correctly. Note that I have a textbox, LotSearchTextbox, where I enter the portion of the Lotnumber that I want to use as the filter. I also have a button, LotSearchButton:

Private Sub LotSearchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LotSearchButton.Click
Try
Dim SomeNumber As Integer = Me.LotCalcinerO2TableAdapter.FillByLotNumber(Me.CalcinerDBDataSet.LotCalcinerO2,[code].....

why the query works in the Query Builder but not in the code?

View 4 Replies

SQL Query - Execute A Sql Query That Will Display The Top 5 Based On Average?

May 17, 2012

I have a data table that looks like this:

USERNAME ICOUNT IAVERAGE DATE_LOGGED
LAGX01 1245 1245 05-07-2012
LAGX02 2211 1422 05-07-2012
LAGX03 1698 1112 05-07-2012
LAGX04 4598 1555 05-07-2012
LAGX05 2589 3245 05-07-2012
LAGX06 3321 1155 05-07-2012
LAGX07 3698 3458 05-07-2012
LAGX08 2589 4587 05-07-2012
LAGX09 1598 1142 05-07-2012
LAGX10 3156 1987 05-07-2012
LAGX11 5547 2011 05-07-2012
LAGX12 9456 3459 05-07-2012

Now, I want to execute a sql query that will display the top 5 based on average, so i did this:

SELECT DISTINCT USERNAME,IAVERAGE FROM myTable WHERE IAVERAGE > 0 AND DATE_LOGGED='05-07-2012' ORDER BY IAVERAGE LIMIT 0,5 and the result is:

USERNAME ICOUNT IAVERAGE DATE_LOGGED
LAGX03 1698 1112 05-07-2012
LAGX09 1598 1142 05-07-2012
LAGX06 3321 1155 05-07-2012
LAGX01 1245 1245 05-07-2012
LAGX02 2211 1422 05-07-2012

Now, I want to numberize my query result, so it will look like this:

RANK USERNAME ICOUNT IAVERAGE DATE_LOGGED

1 LAGX03 1698 1112 05-07-2012
2 LAGX09 1598 1142 05-07-2012
3 LAGX06 3321 1155 05-07-2012
4 LAGX01 1245 1245 05-07-2012
5 LAGX02 2211 1422 05-07-2012

View 4 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

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

Execute Sql Query On .net?

Jan 30, 2012

ALTER DATABASE test SET ENABLE_BROKER this is the sql query i wish to execute while program is executing.because i do not want to everytime i change computer also need open sql management tool to execute this query rath

View 3 Replies

Execute A Query In Program?

Oct 19, 2009

There is a "submit" button and a textbox, when the button is pressed, i want to connect to access database and search for the entered text in the textbox . if true i want to show "okay" a string .

View 4 Replies

Execute A Query In VS 2008

Aug 22, 2010

How to execute a query in visual studio 2008. What is the smooth process?

View 2 Replies

Execute Non Query Error?

Feb 18, 2010

i got an execute non query error... heres the

Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim da As New OleDb.OleDbDataAdapter
Dim ds As New DataSet
Dim obj As New OleDb.OleDbCommand

[code]....

View 4 Replies

Execute A LINQ Statement Without Assigning It To A Variable?

Apr 15, 2011

I have the following function

Public Function GetPositionBySchoolID(ByVal SchoolID As Integer) As CPAPositionsDataTable
Dim positions As New CPAPositionsDataTable

[Code]....

And I was wondering if there is any way to cut out assigning the LINQ result to tmp and just work with the result directly, i.e.

Public Function GetPositionBySchoolID(ByVal SchoolID As Integer) As CPAPositionsDataTable
Dim positions As New CPAPositionsDataTable

[Code].....

View 2 Replies

Query If It Is More Than 1 Row - Execute Start.show()

Apr 29, 2009

I'm trying this for long time and I still can not figure it, I'm trying to query a database "password" with the field to check if the login is there "login" and if it find the login in the table execute start.show() but I still geting errors.

code.

Imports System.Data.SqlClient
Imports System.Data
Public Class login

[CODE]...

View 4 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

Execute My Query To Insert Records In Tables?

Aug 17, 2009

How can i execute my query to insert records in tables?[code]...

View 7 Replies

Make Property Or Directly Execute Query?

Jul 7, 2010

Public Class Customer
Private m_CID As Integer
Private m_FirstName As String

[Code].....

My question is that do I need to add an OID in customer class or I can directly send a query to customer table when ever an order is made.

View 1 Replies

VS 2008 Linq Possible To Use Query In Another Query?

Apr 16, 2011

I was wandering is it possible to use a query within another query below is the code I am trying to use.

Public Function GetInventByComp(ByVal CompID As String) Using DC As New DataClassDataContext
'need to get company id's based off names? thats bad should be name from Id need to rethink this
Dim invent = (From C In DC.Inventors_Companies _
Where C.CompID = CompID _
Select C.InventorID).ToString

[Code]...

I was trying to us multiple values in a string like "1, 3, 5" but I can't seem to get that working either so I am trying to use just a single value now. Can anyone help me? "Yes I am new to this"

View 5 Replies

Trigger Database - Execute Automatically - Query In Code

May 17, 2012

Whenever I run the form and complete the procedure then I need to go back to sql database and click on execute. I want to avoid everytime going back and executing each entry because if I don't execute everytime then the further other [b]form data [/b]will not be[b] retrieved[/b] properly as the [b]previous form[/b] will [b]not be updated[/b]. I hope ur understanding ...

[Code]...

View 1 Replies

Use OleDbDataReader To Execute Union Query In MS Access Database

May 6, 2010

I'm new to VB.NET. I'm trying very hard to not go screaming back to VB6...... I have a Query stored in an Access2000 database called 'build_VNMON'. (Does that make it a Stored Procedure ?) It uses multiple tables and multiple Union Statements to arrive at the desired results. It may not need to be as convoluted as it is, but I inherited it, and it works. What I really need is to output one column of the build_VNMON result to a text file, so I'm trying to use an OleDbDataReader.

[Code]...

View 2 Replies

Create A Module That Will Contain All General SQL Functionality Like Connect - Disconnect And Execute A Sql Query

Sep 8, 2010

I'm fairly new to VB.NET and I'm looking to create a module that will contain all general SQL functionality like connect, disconnect and execute a sql query etc. I think I'm almost there but the code keeps bombing in one place. Can anyone see what is wrong with the following code?

[Code]...

View 1 Replies

Error : "You Have Tried To Execute A Query That Does Not Include The Spec Ified Expression 'ID' As A Part Of The Aggregate Function

Jul 1, 2009

I am working on an application in Visual Basic Express using an Access 2000 database.I am trying to get athe following SQL query to work but get an error in Access.

The query is:

SELECT [ID], [Title], [Author], [Series], [YearPublished], [ISBN], [CoverPrice], [Style], [Condition], [Signed], [Comments], Count([Title]) AS CountOfBooks, Sum([CoverPrice]) AS TotalCoverPrice
FROM tblBooks
WHERE ID=[@ID];

The error is:"You have tried to execute a query that does not include the spec ified expression 'ID' as a part of the aggragate function."

The query works fine without the 'Count' and 'CoverPrice' count and sum to the query.

View 1 Replies

.net - Should Use LINQ For This Query

Sep 4, 2009

I've been reading a fair bit about the performance of using LINQ rather than using a for each loop and from what I understand using a LINQ query would be a little bit slower but generally worth it for convenience and expressiveness. However I am a bit confused about how much slower it is if you were to use the results of the query in a for loop.

Let's say that I have a set called 'Locations' and a set of objects called 'Items'. Each 'item' can only belong to one 'location'. I want to link items that are under the same location to each other. If I were to do this using a normal 'For Each' loop it would be something like this:

For Each it as Item in Items
If it.Location.equals(Me.Location)
Me.LinkedItems.Add(it)
End If
Next

However if i was to use LINQ it would instead be this:

For Each it as Item in Items.Where(Function(i) i.Location.equals(Me.Location))
Me.LinkedItems.Add(it)
Next

is the second (LINQ) option going to loop once through the entire 'Items' set to complete the query, then loop through the results to add them to the list, resulting in essentially two loops, or will it do the one loop like the first (For Each) option?

View 2 Replies

Can Do A LINQ Query Of A LINQ Query?

Mar 24, 2012

Is it possible to do the following? [code] Basically I have one Load_Gridview function that is called on all postbacks, and rather than creating a bunch of different cases.I want the filters to stack.My actual code has more filters set up (4 or 5 of the).It all compiles ok but when I run and try to execute with active checked, or a department selected I get the following error. [code]

View 4 Replies

Linq Query In .net?

May 5, 2009

I have below XML file

<?
xml version="1.0" encoding="utf-8" ?><
PageFlow>
<
attributegroup name="Search"><
attributes>

[Code]...

View 2 Replies

Put A Sum() In Linq Query?

Apr 18, 2012

I am looking to sum the "Status" field.

Dim _detailRecords As New DataTable
_detailRecords.Columns.Add("y")
_detailRecords.Columns.Add("z")

[Code]....

I'm also wanting to keep this as an IEnumerable of DataRow because I will be creating a datatable from it with

Dim yyyyy As DataTable = query8.CopyToDataTable

View 1 Replies

Query With Linq To Xm?

Aug 13, 2010

I am Fairly new to Linq and I am trying to write a simple query to return the error messages within my xml file.

<?xml version="1.0" encoding="utf-8"?>
<Error xmlns="urn:xxxxx">
The following errors were detected:

[code].....

View 1 Replies







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