Asp.net - Make A Dynamic Where Clause: Linq To Sql?
Jun 28, 2011
I have the following linq query:
Dim q = From definition In definitionList _
Where definition.ValueDefName.Contains(criteria)[code]....
I have already looked into this answer: LINQ - dynamic WHERE clause?Unfortunately, it doesn't work for me using .net 4.0. When I attempt to pass the criteria string in it ask for a predicate. The end goal is to add any of these:
definition.ValueDefname.Contains(criteria)
definition.ValueDefDesc.Contains(criteria)
definition.Aliaslist.Contains(Criteria)
definition.StrategicInitiative.Contains(Criteria)
to be passed into the query depending on what checkboxes the user has selected. How can I create a dynamic where clause in linq to sql? Is there new syntax for passing in a where clause as a string?
View 2 Replies
ADVERTISEMENT
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
May 10, 2012
I'm currently trying to loop through an array (two values) and use both values in a query inside the loop Right now my code doesn't work. I'm trying to populate dynamically the "appType" parameter within the "SelectParameters" tags of the SQLDataSource, but this won't work.
[Code]...
View 1 Replies
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
Apr 23, 2009
Pretty new to VB.Net. Having a bit of trouble here with something I though should be simple.Keeping it simple. Let's say i have a Document table with "Name" that I want to search on (in reality there are several other tables, joins, etc ..). I need to be able to build the query where clause based on string values passed in.
Example - user may pass in "ABC" or "ABC DEF" or "ABC DEF GHI".The final query would be (syntax not correct i know)Select * from Documents Where Name Like %ABC% AND Name Like %DEF% AND Name like %GHI%
So, I though I could do something like this.
Dim query = From document In _context.Documents
<< loop based on number of strings passed in >>
query = query.Where( ... what goes here?? )
For some reason, being brain dead or something, I can't figure out how to make this work in VB.Net, or if I'm doing it correctly.
View 5 Replies
Nov 25, 2009
Dim dataContext As New ACSLostFoundEntities()
Dim query = From s In dataContext.FosterForms() _
Join c In dataContext.Fosters() _
On c.License Equals s.License _[code]....
At the end of this I want to do a Where WantedPets.Contains(criteria), is this possible?
View 1 Replies
Aug 25, 2011
Whats wrong with the below query, I am getting this error: Nullable object must have a value.
[Code]...
View 3 Replies
Feb 7, 2012
I have following code that creates Linq query.
I've never used Linq until today and having problem with "Order By Clause"[code..]
If I run the code, I get following error.Name 'p' is either not declared or not in the current scope. How come p!Weight in "Select Clause" works but not in "Order By Clause"?
View 1 Replies
Apr 23, 2009
Let's say I have Plans and Documents
Dim myPlans = _context.Plans.Where(predicate1)
Dim myDocuments = _context.Documents.Where(predicate2)
I have structured the where clause for each using PredicateBuilder. So, myPlans and myDocuments have a correct SQL statement. What I'd like to do is join these two tables into one linq statement. The problem I'm having is that, by default the AND condition is joining the where clauses.
myPlans Where clause : (p.name like "%test%" AND p.name like "%bed%") OR (p.description like "%test%" AND p.description like "%bed%")
myDocuments Where clause : (d.name like "%test%" AND d.name like "%bed%") OR (d.description like "%test%" AND d.description like "%bed%")
When I combine the two the desired result Where clause is:
Where (d.ID = p.ID) AND
(myplans where clause above) OR (mydocument where clause above). Meaning, I'd like the two where clauses in each of the tables to be "or" instead of "And".
The current result where clause is:
Where (d.ID = p.ID) AND
(myplans where clause above) AND (mydocument where clause above). Meaning, I'd like the two where clauses in each of the tables to be "or" instead of "And".
I'm forming the statement like this:
Dim test = From d in myDocuments _
Join p in MyPlans on d.ID Equals p.ID _
Select d.Name, p.Name
View 2 Replies
Mar 17, 2009
Right now our queries can be driven through the DataTable Select statement. However, utimately we want to end up with more complex queries. We think Linq is the answer, but can't find anyexamples of this.Here is the sysnopsis:I have a dataset containingdatatable(s) of informationI have a datatable containing business rules as strings like:
(ISNULL(MEMLNAME, '') = '')
(#1880/01/01# <= CONVERT(BIRTHDATE, 'System.DateTime') AND CONVERT(BIRTHDATE, 'System.DateTime') <= #2009/12/31#)
[code].....
View 3 Replies
Jun 13, 2012
So I have a query I'm doign where, at the moment, the places to search are set in stone, but the values that maybe be searched for is variable.Right now, I have a drop down menu that allows you to choose a character class. This limits the items to a specific class. Then I can filter on text from a textbox.
However, each item in the list has a source, based on the book it is published in. I want a user to be able to choose several from a checkbox menu, then pass those values for inclusion in the criteria for finding items.
The problem I'm having is finding the most concise way to do dynamic WHERE clauses. There are several examples out there, and even a couple don't seem to want to do exactly what I'm doing, and I'm having trouble determining if it will work for me.
View 8 Replies
May 24, 2010
I need to represent the following query using LINQ:
DECLARE @PurchasedInventoryItemID Int = 2
DECLARE @PurchasedInventorySectionID Int = 0
DECLARE @PurchasedInventoryItem_PurchasingCategoryID Int = 3
DECLARE @PurchasedInventorySection_PurchasingCategoryID Int = 0
[code]....
Now, I know that a query in .NET doesnt look like this, this is my test in the SQL Design Studio. Naturally VB.NET variables will be used in place of the SQL local variables.My problem is this: All of the conditions after "WHERE" are optional. In that a query might be made that uses one, some, all, or none of the conditions. V.PropertyID and V.Value can also appear any number of times.In VB.NET I can make this query easy enough by simply concatenating strings, and using a loop to append the "V.PropertyID/V.Value" conditions.I can also make a Stored Procedure in MS SQL, which is easy enough.However, I want to accomplish this using LINQ.
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
Nov 8, 2011
I have the following xml:
<Root>
<Result img="1.png" name="a">
<Programs>
[Code].....
What is wrong with linq query. Why does a 1 work but a 2 does not? I would also like xml structure preserved after filtering.
View 1 Replies
Jun 7, 2011
Here's what I want to do:
Dim queryX = From m In db.Master
Where m.Field = value
Group Join d In db.Detail
On m.Id Equals d.MasterId Into Group
Where d.Field = value
In English, I want to join the master and detail tables, specifying conditions on each. But this causes the following compiler error:
"Name 'd' is either not declared or not in the current scope."
It works if I put this same condition in functional form:
Group Join d In db.Detail.Where(Function(x) x.Field = value)
but I think this is more typing, harder to understand, and introduces that irritating dummy variable. I really would prefer to use the query comprehension syntax. Is there a way to accomplish this?
View 1 Replies
May 4, 2012
I am trying to query for records where that column IS NULL:
Dim UnassignedSubSvc =
From c In CurrentContext.SubService
Where c.Product.ProductSubServiceId **is null**
Select c).ToList()
View 1 Replies
May 25, 2011
I have a LINQ to sql statement that joins 2 tables. I would like to add a order by clause on one of the columns. However the order by clause does not seem to take effect at all. right syntax in VB.net to achieve order by in the following:
Dim query = From dtIt In dbsomecontext.mytable
Join dtIl In dbsomecontext.anothertable On dtIt.ItemID Equals dtIl.ItemID
Where dtIl.IsAvailable = True
Order By dtIt.manufacturer
[Code]...
View 2 Replies
Aug 19, 2010
i want to use a if, then statement in a where clause in linq to sql. Is this possible? I'm trying to achieve the following:
[Code]...
I'm trying to only add the And If if intLink_pk > -1. If it = -1, then i don't want the and clause at all. Is this posssible within the linq to sql itself?I understand i can write 2 entirely different selects, but if I can't get this done, I may have to write about 50 of them as I want to use multiple Ands if certain conditions are met on the form, such as chk boxes etc.
View 3 Replies
Jun 22, 2012
I have a combo with 6 text NA,PO,FA,GO,VG,EX and I want to do a Linq query upon the selected value in this combo used as field in the where clause. for example:
'if "NA" is selected :
Dim query = from t in db.table where t.NA > 0 Select t
'If "PO" is selected :
Dim query = from t in db.table where t.PO > 0 select t
How to do this in elegant way without writing 6 if statement or using "Select case"
View 1 Replies
Oct 18, 2010
how to translate a simple SQL LEFT OUTER JOIN with a two condition where clause into a working Linq-to-Entities query. There are only two tables. I need values for all rows from Table1, regardless of matches in Table2, but the WHERE clause uses fields from Table2. In SQL, the two parameters would be Table2WhereColumn1 and Table2WhereColumn2, and the query (which works) looks like this:
SELECT t1.Table1Id,
t1.FieldDescription,
t2.FieldValue
[code].....
I've tried using Group Join with DefaultIfEmpty(), as well as an implicit join (without the actual Join keyword), and I only get rows for items that have values in Table2. I'm sure this won't help, but here's an example of the Linq I've been trying that doesn't work:
Public Shared Function GetProfilePreferencesForCedent(ByVal dc As EntityContext, _
ByVal where1 As Int32, _
ByVal where2 As Int32) _
[code].....
View 1 Replies
Sep 10, 2009
Example-I have a person class
Public Class Person
Private _fname As String
Public Property Fname() As String
[Code]...
View 2 Replies
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
Sep 14, 2010
How would I get something like this to work so that I can dynamically alter the where-clause in this linq to sql query?
Dim AccountID = 1234
Dim AccountList
Select Case Types
[Code]....
With the above code I get this error:
Late binding operations cannot be converted to an expression tree
View 1 Replies
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
Feb 24, 2012
UPDATE: Groo's answer was marked correct as it would be a good alternative. I decided to stick with the select/switch statements to avoid the performance issues of using reflection. As far as I can tell there is no way to do what I want without using Dynamic LINQ (and reflection) or a decompiler (to manually code each LINQ statement, which I am already doing by hand). Note: to make this update DropDownList16 through DropDownList20 were removed (needed the characters).Is there a way to abstract the building of a dynamic Linq to SQL query?
I am trying to build a Linq to SQL query with a dynamic where clause, based on user supplied filters. The user needs to be able to filter strings, integers, and dates using advanced options (equal, not equal, contains, starts with, etc). The user needs to be able to use these filters with as many or few columns as desired.
[Code]...
View 2 Replies
Jan 5, 2010
Dim query=Form f in BBSDb.Forms order by f.FormID Descending Select f where I want the f.FormID to be able to replace with a variable..What I mean is.. it doesn't have to f.FormID .. I may want to set f.FormName to be in descending for example using the code dynamically.. Is there anyway to achive?
View 2 Replies
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
Oct 17, 2011
This is an extension of my last question which was answered, so I am starting a new thread. I have a query in LINQ that I was given to trim down the columns of a datatable. This works well, however, I need to make the LINQ query dynamic. So the query below:
Dim qColumnsIWant = From row As DataRow In inDT.AsEnumerable() _
Select _
EPN = row.Field(Of Integer)("EPNID"), _
EPNID = row.Field(Of String)("EPNNumber"), _
Ingot = row.Field(Of String)("Ingot"), _
ShopOrder = row.Field(Of String)("ShopOrder")
View 5 Replies
Jan 24, 2010
I have been looking into dynamic querying of datasets and the use of copytodatatable() for a project. I have seen in alot of forums that the copytodatatable functionality is unusable as it was removed. have an example of a dynamic Linq query being loaded into a datatable.
View 1 Replies
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