Convert This String-based Sql Query To Use Linq?

Feb 3, 2011

I have the following routine (that works) but which is messy to update owing to the hand-typed strings it uses:

Private Sub ListDefaults()
Dim conn As New SqlConnection( _
"server=bas047AUTODESKVAULT;Database=DWGDetails;Integrated Security=SSPI")
'Dim conn As New SqlConnection( _

[code]....

View 1 Replies


ADVERTISEMENT

Use LINQ Query To Convert DBNull To String?

Oct 12, 2009

I've created a small WPF app in VB9 to catalog our sizable DVD collection. This exercise has been more about learn programming than the need to have a movie database app, but I digress.The SQL database I created has a field for "Genre" that does not have an entry for all the movies in our collection. As such, when I run the method that filters the movie listing by "Genre" the compiler throws the "StrongTypingException was unhandled" error stating "The value for column 'Genre' in table 'Movies' is DBNull.".

I now understand where the DBNull problem comes from and have been searching for several hours to find a VB solution that converts the DBNull fields to strings on the fly during the LINQ query. I've found many solutions people have tried but, since I'm quite new to all this, none of them have worked for me. Pasted below is the method that throws the exception.

[Code]...

View 2 Replies

VS 2008 Convert A String Into A Linq Query?

May 20, 2012

Is there a way to convert a string into a linq query? Searching for those words brings up tons of stuff unrelated to what I'm trying to do..For instance, this works, of course...

Dim lstMyQuery = (From item in htMyTable.values where item(0) = "Apple" Select item)

But somehow I want to do this:

Dim strQuery = "From item in htMyTable.values where item(0) = " & CHR(34) & "Apples" & CHR(34) & " Select item"
Dim lstMyQuery = (Somehow run/convert strQuery as a query)

The reason is strQuery will be different each time depending on what the user has selected earlier in the program.R if lstMyQuery is already a collection, is there a way to add to it?

Dim lstMyQuery = (From item in htMyTable.values where item(0) = "Apples" Select item)

(this obviously doesn't work, but it's basically what I want to do...)

lstMyQuery.Add(From item in htMyTable.values where item(0) = "Oranges" Select item)

View 3 Replies

Create A LINQ Query Based On SQL?

Sep 25, 2010

i try to convert this kind of sql query into linq in visual basic, but i get stuck on how to make a percent.. I also don't know how to use linqpad for creating this linq

SELECT CASE RIGHT(PICName, 3)
WHEN '(P)' THEN 'Problem'
WHEN '(R)' THEN 'Request'

[Code].....

View 3 Replies

Asp.net - Select Query In LINQ Based On Foreign Table

Mar 2, 2011

I have 2 Tables , OrderDetails and Requests In my LINQ to SQL dbml file. OrderDetailsID is a foreign key in Requests Table.

I want to write the query to get the sum of UnitCost from OrderDetails based on OrderId. And If there is a row in Requests Table for each OrderDetailsID, and the Requests.Last.RequestType="Refund" I want to reduce the total refund amount from the main sum otherwise If there is no row based on OrderDetailsID, add to sum.

Here is the way I implement that. I am looking to prevent using "For each".

iRefund = (From od1 In dc.OrderDetails _
Where od1.OrderID =1 _
Select od1.UnitCost).Sum

[Code]....

View 1 Replies

Cannot Filter A Binding Source Based On A Linq Query

Feb 3, 2010

I've got a datacontext and created a binding source using LINQ to sql but cannot filter said bindingsource. The supportsfilter of the bindingsource is set to false ? why ? I can't find any info to say that I cannot filter a binding source based on a linq query.[code]

View 3 Replies

Select Query In LINQ Based On Foreign Table?

Mar 4, 2009

I have 2 Tables , OrderDetails and Requests In my LINQ to SQL dbml file.OrderDetailsID is a foreign key in Requests Table.I want to write the query to get the sum of UnitCost from OrderDetails based on OrderId.And If there is a row in Requests Table for each rderDetailsID, and the Requests.Last.RequestType="Refund" I want to reduce the total refund amount from the main sum otherwise If there is no row based on OrderDetailsID, add to sum

View 3 Replies

Change A Global Variable Value Based On Linq Query While Executing?

Nov 27, 2010

I have two module-level variables and Linq query. I want the results of the Let clause to change the global variables - is this possible?

For example:
Dim X As Integer = 0
Dim Y As Integer = 0

[code].....

View 3 Replies

Update One Entity Based On Results Of Another LINQ-to-Entities Query

Apr 9, 2011

I have the following LINQ-to-Entities query:[code]This will give me all the resident assignments for the current year/term. Then I have this LINQ-to-Entities query:[code]This will give me all the rooms. I want to iterate through the assignments and based on what room is assigned update the occupancy in reset_occupancy. I'm not 100% sure how to accomplish this. Here is my pseudo code of what I want to accomplish:[code]

View 1 Replies

Update One Entity Based On Results Of Another LINQ-to-Entities Query?

Dec 15, 2009

I have the following LINQ-to-Entities query:

' Get all the residency assignments that match the term/year.
Dim assignments = From p In dbContext.Residents _
Where p.semester = term _

[code].....

View 1 Replies

How To Convert SQL Query Into LINQ

Dec 8, 2011

I am new to LINQ, how to convert the following SQL query to LINQ?
SELECT
Date,ShiftName,Max(Score) AS Score, 1 AS IsPreferred
FROM
Temp_Nurse_Fill_RequestNumbers
group by
ShiftName,date

View 4 Replies

How To Convert The SQL Query Into LINQ

Dec 8, 2011

I am new to LINQ.convert the following SQL query to LINQ?

SELECT
Date,ShiftName,Max(Score) AS Score, 1 AS IsPreferred
FROM

[code]....

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

Convert SQL Query/subquery To LINQ (VB)?

Jan 16, 2012

Here is my SQL query..

SELECT EqID, MakeID, Model, Description,
(SELECT MAX(EvDateEnd) AS MaxOfDateEnd
FROM tblEvent WHERE (EqID = tblEquip.EqID) AND (Event = 'REG')) AS RegExpire,

[code]....

For example, I want to get the Max EvDateEnd from the Events table for each record in tblEquip, but not all records in tblEquip have the "REG" event, while others have multiple REG events.Some events (like EAE) I leave the tblEvent.EvDateEnd field blank to show the event is open and ongoing, such as issuing a vehicle to an employee(entity). when the vehicle is returned I enter the date in EvDateEnd essentially closing the event.

View 1 Replies

Convert A LINQ Query Resultset To A DataTable?

Jul 20, 2010

How can i write this query with LINQ to a FoxPro database?SELECT count(*) FROM Table group by item1I wrote it as below, but it doesn't work

Dim Query
Dim dt As New DataTable
Dim da = New Odbc.OdbcDataAdapter("SELECT * FROM table1",connection)

[code].....

View 3 Replies

Asp.net - Convert SQL (Pseudocode) To Dymanic LINQ Query From List<Custom> Parameters?

Apr 5, 2012

GOAL: Bind nested ListViews to LINQ generated iQueryable of anonymous type. I want to use LINQ because you can use GroupBy and bind the nested ListView to the 'it' keyword.

SETUP: I have groups of sets of conditions. Each set of conditions is stored in the BillingCodes table. Each group of BillingCodes is stored in the BillingGroups table.

I have a custom object that stores the ID, Name, and NumCodes for each BillingGroup that the user has chosen.I have a collection of these objects called GroupsList that has a list of the groups that the user has chosen.

Problem 1: I can iterate through GroupsList and grab all the IDs. How do I translate the SQL 'WHERE ID IN(a string of comma delineated IDs)' for LINQ to SQL? Is that the best way to do that?

Problem 2: Once I have the list of BillingGroups I need to iterate through each group. For each group, I need to iterate through the BillingCodes. For each BillingCode I need to generate a WHERE clause that has all of the conditions in the BillingCode. I propose something like so:

for each BillingGroup in BillingGroups
for each BillingCode in BillingGroup.BillingCodes
where1 = "..."
next
next

Problem 3: Here's the part where I don't have a clue. I need to dynamically create a query in LINQ to SQL. Keep in mind that I don't know how many groups there'll be or how many codes are in each group.

There are 2 tables:

**transactions**
transaction_id
patient_id

[code]....

View 1 Replies

Convert A Two-table Exists Query From SQL To Linq Using Dynamic Fields In The Subquery?

Jun 7, 2011

I'm trying to query old Access database tables and compare them with SQL Server tables.They often don't have primary keys, or they have extra fields that had some purpose in the nineties, etc., or the new tables have new fields, etc.I need to find records - based on a set of fields specified at runtime - that are in one table but not another.So, I do this kind of query all the time in SQL, when I'm comparing data in different tables:

dim fields_i_care_about as string = "field1, field2, field3"
'This kind of thing gets set by a caller, can be any number of fields, depends on the
'table
dim s as string= ""

[code]....

It tells me it can't convert a Boolean - Is there any way to do this without Linq expressions? They seem far more complex than what I'm trying to do here, and they take a lot of code, and also I can't seem to find examples of Expressions where we're comparing two fields in a subquery.Is there a simpler way? I know I could do the usual EXISTS query using JOIN or IN - in this case I don't need the query to be super fast or anything. And I don't need to use a DataTable or DataSet - I can put the data in some other kind of object.

View 1 Replies

Dynamic LINQ Query Based On Dynamic Number Of Comboboxes?

Feb 5, 2010

I would like to bind a DataGridView to a different LINQ query every time (in order to reuse the same form/DGV for different queries), like this:

[Code]....

but the "Qry", and the number of comboboxes, and their field names would change every time.

View 5 Replies

C# - Using LINQ To SQL To Query A Word Started With Something Within String?

Dec 7, 2010

I'm using LINQ to SQL to query some data.Here's my Employees table colummns:

-FirstName (or givename i.e. John)
-LastName (has middle name and family name seperated by space i.e. Mac Tenny

Some people have middle name and some don't. Here's some sample data.

-FirstName LastName
John Mac Tenny (with middle name "Mac")
Mike Mika (no middle name)

[code]....

I want to query for people who have their middle or last name started with search criteria. Here's how I search for employees with middle name started with
"joe" :

query = From emp In db.Employees _
Where emp.LastName.StartsWith("joe") _
Select emp

How do I query for employees with family name(last part of the LastName) started with "joe"?

View 2 Replies

Assign Result (from LINQ To XML Query) To List(Of String)?

Jul 6, 2011

Is it wrong? Please suggest me correct way.

Public Function ReadXML() As List(Of String)
Dim list As New List(Of String)
Dim xmlDoc As XDocument = XDocument.Load("C:\MappingFile.xml")

[code].....

View 1 Replies

Linq Query To Compare If Collection Contains List Of String?

Feb 3, 2012

I am using a path comparison to find any children tags which works well when there is only 1 tag selected. This is my code for when multiples tags are selected but it is not working. Can you point me in the right direction?

Single Tag Selection (working)
Dim tagpath = uxTags.SelectedItem.Text
lnqCases = From i In lnqCases Where i.HelpDeskTagItems.Any(Function(x)

[code]...

View 1 Replies

Using LINQ To Query XML And Return Empty String If No Data Is Present?

Mar 8, 2011

What I'm trying to do is to write a generic rss reader that I plug in any URL into without worry if the feed has all the common properties. For instance in my example below I'm looking for pubDate, however if no pubDate exists in the xml I'd like to return the current Date. I cannot seem to get the syntax right though.

Dim xmldoc As New XDocument
xmldoc = XDocument.Load(url)
Dim feeds = From feed In xmldoc.Descendants("item") Select New With { _

[code].....

View 2 Replies

Convert A Query Answer To A String To Display?

Mar 18, 2010

I have this query that returns the number of rows which is :

SELECT COUNT(*) AS MemberTotal
FROM Members

i'm trying to write a readonly property called count to return that value in string its suppose to display the result of my query in a tool strip status label, I have this in my code

Public ReadOnly Property count() As String
Get
Return memberAdapter.GetMemberCount()
End Get
End Property

but it gives me an error saying I can't convert dataset to string something like that and I don't have any idea on how to convert it I tried the Cstr but that didn't work?

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

Filter Parent / Child Table In Linq Query Based On Entities In Child Table?

Jul 9, 2010

I have a table (Projects) which is linked to projectVersions on projectID..projectVersions contains several columns on which I'd like to filter a returned project (and associated projectVersions) list. For example there is a "capacity" column and a "country" column. I am doing a filtered list of projects on one page and I'd like to include all projects where any one of the associated projectVersions has a capacity of 750ml and a country of "France" for example.It may be that a particular parameter is not set and so I pass a zero to indicate not to filter on that.I guess this needs some kind of subquery as when I try and do something like this: [code] it doesn't work as the "xxx" bit, being one-to-many, expects me to specify an item to get at the entities within and yet I want to query where ANY of the associated versions match one of the criteria.

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







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