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


ADVERTISEMENT

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

C# - Use Meta Tales Or Table Names To Construct A Dynamic Query In LINQ?

Feb 8, 2011

Is there a way to use Meta Tales or Table names to Construct a dynamic query in LINQ?

foreach (var metaTable in db.Mapping.GetTables())
{
var queryType = metaTable.RowType.Type;

[code]....

Is there a way to do something like this? The attempt above yields the error:Could not find an implementation of the query pattern for source type 'System.Data.Linq.ITable'. 'Select' not found. Consider explicitly specifying the type of the range variable 'q'.

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

Use Dynamic Fields Over All Of The Tables In A Dataset In LINQ?

Jan 13, 2011

How can I use dynamic fields in LINQ in the PART OF THE where query? I mean that I want to write where x = *dynamic fields* I want to do the select query in all of the tables that are in the dataset, not on one datatable.

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

.net - In Linq To Entities - How To Do An Inner EXISTS (SELECT 1 FROM - Type Of Query

Mar 6, 2011

I am trying to write a LINQ equivalent of

SELECT C1, C2, C3
FROM T1
WHERE T1.C4='xyz' AND
EXISTS (SELECT 1 FROM T2
WHERE T1.C17 = T2.C24)
ORDER BY C3

I'm using EF CTP 5, so I have a DBContext variable named dbc, which includes DBSet objects T1s and T2s, based on POCOs T1 and T2.

In LINQ I write

DIM IND = From i In dbc.T1s
Where i.C4 = "xyz"
And (From t In dbc.T2s Where i.C17 = t.C24).Any
Select i.C1, i.C2, i.C3
Order By C3

Running the query I get the error message "Unable to create a constant value of type 'T2'. Only primitive types ('such as Int32, String, and Guid') are supported in this context." When I omit the inner expression (third line in the LINQ code), the query runs fine. I tried switching the orders of the inner comparison, to be t.C24 = i.C17, with no effect.

View 1 Replies

.net - Dynamic Linq Query?

Mar 14, 2011

I have been trying the following but it returns unexpected results:

Dim xd As XDocument = _
<?xml version="1.0" encoding="utf-8"?>
<root>

[code].....

The above result returns both 'element' nodes however it should only return the first where element/subelement@id=1/subsubelement@id=3 However if results.Ancestors. is used it returns the correct 'subelement' and if that line is not included it returns a single 'subsubelement' whih is also correct I don't understand why when mvoing to the 'element' it returns both (I realise both have a subelement with id=1 but I thought each further query would filter out the presvious results)?

View 1 Replies

Dynamic Query Linq To Xml?

Mar 16, 2011

I want to write a query that the "where" in the query is a string something like"

Dim query as string= "Name =xxxx and Date > 10 "
Dim t = from book in doc.Descendants("books") Select _
[Name] = book..value, [Date] = book..value....
Where (query)

I build the query string on run time

View 2 Replies

Optimize Linq To SQL Query, Group By Multiple Fields

Jul 26, 2010

My LINQ query contains the following Group By statement:

Group p By Key = New With { _
.Latitude = p.Address.GeoLocations.FirstOrDefault(Function(g) New String() {"ADDRESS", "POINT"}.Contains(g.Granularity)).Latitude, _
.Longitude = p.Address.GeoLocations.FirstOrDefault(Function(g) New String() {"ADDRESS", "POINT"}.Contains(g.Granularity)).Longitude}

The query works, but here is the SQL that the clause above produces

SELECT [t6].[Latitude]
FROM (
SELECT TOP (1) [t5].[Latitude]

[Code]....

but this produced an error: "A group by expression can only contain non-constant scalars that are comparable by the server."

View 1 Replies

Building A Dynamic LINQ Query

Mar 16, 2011

I have a listbox which users can select from a list if Towns, I want to be able to build a LINQ query based on the selected items in the list e.g.

[Code]...

View 2 Replies

C# - Linq Query With Dynamic Where Clause?

Apr 13, 2012

I'm need to do the equivalent of a t-sql statement like this using LINQ:

SELECT * FROM mytable WHERE idnumber IN('1', '2', '3')

so in LINQ I have:

Dim _Values as String = "1, 2, 3"
Dim _Query = (From m In mytable Where idnumber = _Values Select m).ToList()

Not sure what to do with _Values to make idnumber evaluate each value in the string.

View 2 Replies

Linq Dynamic Query Program?

Aug 9, 2010

How do i great dynamic linq query in VB.net?

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

Check If LINQ Entity Of Unknown Type Exists In Its Respective Table

Sep 23, 2010

I'm trying to check if a LINQ Entity exists in its table, but at design time I dont know what type that entity is. So I figure I'll just get the table, and try the Contains method on it. But I cant get the table in such a way that I can query it at design time.I've tried the GetTable method on the datacontext, but I dont know how to cast it to the appropriate type when using GetTable(Of). GetTable(Type) works, I just use Entity.GetType(), but then I don't know how to query the ITable thats returned.To try and cast the ITable to something useable, I created an interface(IWhatever) that could implement properties that are native to all of my entities I would encounter. [code]

View 2 Replies

Handle Null In LINQ Subquery?

May 24, 2009

I've got a subquery that returns the most recent value from a child table. In some cases the subquery returns nothing. The query below fails at runtime because the inferred type of MemberPrice is decimal and is not nullable. [code]...

View 4 Replies

Query From A Table That Criteria Fields Are Contain Unicode Character

Jun 12, 2011

I have a problem with unicode character, as i create a database in SQL Server 2005. I want to query from a table that criteria fields are contain unicode character i.e NVARCHAR.

e.g: SELECT * FROM tblMyTable WHERE MyField='ភាសាខ្មែរ'. But when i did like this all rows that contain Unicode Character always display. it doesn't fit to my criteria

View 1 Replies

Dynamically Select Table At Runtime With Dynamic LINQ?

Feb 17, 2011

I'm developing an application to allow engineers to conduct simple single table/view queries against our databases by selecting Database, Table, Fields.

I get how to use the Dynamic LINQ Library Sample to provide for dynamically selecting the Select, Where and Order by Clauses at runtime but I'm at an impass on how to allot for table choice.

Is there a way to provide for dynamically selecting the "from" table at run time

EDIT

So Both of the answers seem to be saying the same general Idea. I'm going to try to convert the C# into VB and get it to work.

The first answer converts to

NotInheritable Class DataContextExtensions
Private Sub New()
End Sub

[Code].....

View 2 Replies

.net - Write A Linq To Sql Query To Find Records Where Field Name Can Match One Of Dynamic Number Of Strings?

Mar 21, 2012

I have users check off lab facilities in a UI. I want to use linq to fetch corresponding records for all of the labs that they have checked off. Basically,

Dim myRecs = (From l As EpiData In myDataContext.EPIDatas Where l.facility= _
one of the checked labs

So basically, I need to write a linq query where the "strings" to match are determined at runtime. Is there any way to do this easily? I know that there is a library out there called dynamic LINQ, but (1) it's in C# and I'm writing in VB (2) I'm really just looking for a single, simple solution for this single case.

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

Linq To SQL / Query From Table

May 18, 2012

I am starting with Linq to SQL in VB.NET, and trying to figure out how to make a simple query to a database.I want to do it all programaticly.I have made a connection to the database with a connectionstring, and this works fine - I can get a message if the database exists or not.But when I want to query a table, I am missing the part where I connest to the table. I have googled a lot to find an answer for thi, but [code]

View 2 Replies

Using LINQ - Query Out Dbf Table?

Sep 2, 2009

I was able to use it for querying out info from an XML file but now i have to use the DBF file from a folder. where i can find out how to query out DBF table using LINQ. I basically need to get data from a column of a DBF table.

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

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

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

Combining Multiple Table Data With Different Fields Into A Single Table With All The Fields?

Feb 13, 2012

how to execute this SQL Statement

Scenario:
Given Tables T_Data, T_AllDesc, T_System1, T_System2
Given Fields:
T_Data= f_id, f_Desc, f_CreationDate, f_CreationTime, f_System1, f_System2

[Code].....

View 7 Replies

DB/Reporting :: Update The Count Of A Field In A Table Via Subquery

May 24, 2008

I wrote some code to update the count of a field in a table, but it is very slow. So I'm trying to do it in a subquery to speed things up, but I'm not very good with subqueries. I'm getting the error "Subquery returned more than 1 value".

[Code]...

View 1 Replies







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