Can't Use A Where Clause On Both Tables In A LINQ Group Join?
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
ADVERTISEMENT
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
Jul 21, 2010
I'm working on becoming familiar with LINQ, and getting my head around the syntax and usage.
[Code]...
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
Aug 19, 2010
I have three tables Student, TimeSheet and TimeRecord.
Talbe columns:
Student : StudentId, FirstName,
LastName
TimeSheet: TimeSheetId,StudentId, IsActive
[Code].....
View 4 Replies
Apr 17, 2011
How to JOIN tables on nullable columns? I have following LINQ-query, RMA.fiCharge can be NULL:
Dim query = From charge In Services.dsERP.ERP_Charge _
Join rma In Services.dsRMA.RMA _
On charge.idCharge Equals rma.fiCharge _
Where rma.IMEI = imei
Select charge.idCharge
I get a "Conversion from type 'DBNull' to type 'Integer' is not valid" in query.ToArray():
Dim filter = _
String.Format(Services.dsERP.ERP_Charge.idChargeColumn.ColumnName & " IN({0})", String.Join(",", query.ToArray))
So I could append a WHERE RMA.fiCharge IS NOT NULL in the query. But how to do that in LINQ or is there another option?
The problem was that the DataSet does not support Nullable-Types but generates an InvalidCastException if you query any NULL-Values on an integer-column. The modified LINQ-query from dahlbyk works with little modification. The DataSet generates a boolean-property for every column with AllowDbNull=True, in this case IsfiChargeNull.
Dim query = From charge In Services.dsERP.ERP_Charge _
Join rma In (From rma In Services.dsRMA.RMA _
Where Not rma.IsfiChargeNull
Select rma)
On charge.idCharge Equals rma.fiCharge _
Where rma.IMEI = imei
Select charge.idCharge
View 2 Replies
Jan 23, 2012
I have three tables: BOOKS(BookID, Title, Author) CLASSES(ClassID, Title, MajorID,BookID) and MAJORS(MajorID, Description). I added these three tables to a LINQ to SQL class (UBooks.dbml) and then connected to the data sources. Then declared DataContext.
Using LINQ, my goal is to display all books in a grid which I have done with:
Dim allBooks = From Books In db.Books _
Order By Books.BookID _
Select Books
Me.BookBindingSource.DataSource = allBooks
Next I need to display all majors in a listbox. I would like the description to be shown however I am only getting the majorID to display with the code
lstMajors.DataSource = db.Majors
I have tried db.Majors.Description, however I am told then that Description is not a member of the LINQ table. and my final goal is to display all the books for whichever Major is selected in another data grid. This is what I am primarily concerned with right now. When using the SQLClient classes, I was able to create a string joining the tables (also had the description properly showing in the listbox, but that is of little importance right now to me). In the video tutorials I was able to watch whenever the tables were dragged from the database into the OR designer of the LINQtoSQL.dbml class file all of the associations were already there and the narrator only really had to do some drag and drop. Not only is drag and drop not working without these associations, but I'd much rather learn the code behind it.
View 4 Replies
Mar 7, 2011
Can anyone help me by translating this SQL Statement to Linq? [code]
View 1 Replies
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
Aug 23, 2010
I have two tables Employees and CafeLogs. Some employees can be cashiers and also customers at the Cafe shop.
Table structures:
Employees: EmployeeId(PK) , FirstName, LastName
CafeLogs: LogId (PK), CashierId, EmployeeId, Value, => CashierId and EmployeeId are the data from column EmployeeId of Empoyee table
Table relationship:
Employees 1:N CafeLogs (CashierId (FK))
[code]....
Right now I know how to select only LogId, Employee's name, and , Value, not with Cashier name yet.
Dim query = From log In db.CafeLogs _
Join emp In db.Employees On emp.EmployeeId Equals log.EmployeeId _
Select log.LogId, emp.FirsName, emp.LastName, log.Value
View 1 Replies
May 17, 2012
Iwant to join two tables,'Employee' and 'Dispatch'. Dispatch has column 'DispatcherID' and 'TechnicianID' which are both foregn keys to EmployeeID in Employee table.I want to join these two tables using EmployeeID so that i can obtain the matching name to each id.but it only works when i make a single join to either DispatcherID or TechnicianID and not for both.
[code]
select Employee.firstname+' '+Employee,secondname as Technician,Employee.firstname+' '+Employee.secondname as Dispatcher from Dispatch inner join Employee on Dispatch.TechnicianID=Employee.EmployeeID inner join Employee on
[CODE]...
View 12 Replies
Oct 29, 2010
In my datagridview I was fill in using join tables (table1 = pr_employees, table2 = pr_employee_deduction). My problem now is during save button I want to update records from table2 only.[code]
View 3 Replies
Aug 26, 2009
I have a table DailyTransaction in MsAccess as under: DTDate, DTTime, CustId, Description, Quantity, Price, Amount, AccountNumber, Name I want to extend following query with a Group By clause:I require to display all records in a Listview Group By CustId.Then I require to display all records in a Listview Group By AccountName.e how I can extend following query with Group By clause to achieve the required results
View 4 Replies
Sep 19, 2010
I've got a query which returns all invoices and credit notes for a given client, in a loyalty structure query where I need the total value of the points the client achieved through invoices, minus those that was forfeited by credit notes. Now I start my query like this (There are more unions in the query, this is just the first part)
[Code]...
View 3 Replies
Jan 29, 2010
Here is a snippet of my (VB) LINQ:
From event_evn In xmlEvents.Descendants("event_evn") _
Join genre_gnr In xmlGenre.Descendants("genre_gnr") On event_evn.Element("evn_gnr_id") Equals genre_gnr.Element("gnr_id").Value _
Group Join eventdata_eda In xmlEventData.Descendants("eventdata_eda") On _
eventdata_eda.Element("eda_evn_id").Value Equals event_evn.Element("evn_id").Value And _
eventdata_eda.Element("eda_dty_id").Value Equals "15" _
[Code]...
View 1 Replies
Sep 1, 2010
I've been using Java for a number of years, and now I'm at the beginning again with VB.NET.How would I go about joining two tables?- order doesn't matter, as long as i can print/display the order
Here's an example of what I'd like to do:
table 1
data 1 , data 2 , date time
12 , 23 , 2010/8/20 10:00:00
[code].....
View 4 Replies
May 19, 2010
I'm trying to do a join on two tables but i'm having a hard time. Here's the table schemes:
table1 : tbl_Trainees_IntentToParticipate
Scheme:
TraineeID (PK)
FName
LName
I want to select all TraineeID/FName/LNames from table1 where TakenTest (from table2)= false
heres the sql statement I have so far:
SqlJoinStatement = "SELECT tbl_Trainees_IntentToParticipate.FName, tbl_Trainees_IntentToParticipate.LName" _
& "FROM(tbl_Trainees_IntentToParticipate, tbl_Trainees_TabScores) " _
[code]....
View 2 Replies
Dec 9, 2010
I am trying to accomplish the following:
SELECT Table1.prod_code, SUM(Table1.prod_bal) AS TotalProdBalance
FROM Table1
LEFT OUTER JOIN Table2 ON Table1.prod_code = Table2.prod_code
WHERE Table2.prod_code IS NULL
GROUP BY Table1.prod_code
HAVING (SUM(Table1.prod_bal) <> 0)
How to translate this to Linq using VB.net syntax?
View 1 Replies
May 23, 2009
how to join 3 tables, I have the following statement but I'm getting a missing a (syntax error "operator error")
da = New OleDbDataAdapter("SELECT [S].[Scheduled Vege], [V].[Description],
[DS].[Task], [DS].[Task Date], [DS].[Completed] FROM [Scheduled] AS S
INNER JOIN [Date Schedules] AS DS ON [S].[SchedID] = [DS].[SchedID]
[code]....
View 3 Replies
Jun 16, 2010
I am working on a Visual Basic 2008 program using an Access 2000 database. My SQL codereturns no records, but I know the table has a record. A simplified example of the code looks like this:
SELECT Table1.Field1, Table1.Field2, Table1.Field3, Table1.Field4, Table2. Field1, Table2.Field2 FROM Table1 INNER JOIN Table2 ON Table1.Field1 = Table2.Field1 AND Table1.Field2 = Table2.Field2 WHERE Table1.Field1=@Field1 AND Table1.Field2=@Field2;
[code].....
View 2 Replies
Nov 30, 2011
Is it possible to join a table from a SQL Server DB and an oracle DB?
View 1 Replies
Mar 14, 2012
here what i try to get:
Table A:
Field: Name A
Field: Id_A
[Code]....
In fact i want to list in grid the table A with a column which contains a checkbox and is checked if the item is include in table B (field TableA_Id_A).
View 2 Replies
May 22, 2012
I am tying to build a COMMAND that joins two tables from two different access databases in vb.net,
View 4 Replies
Jan 27, 2010
I used INNER JOIN to bind the two tables and my problem is how to populate/show on the datagrid.[code]....
View 12 Replies
Jun 21, 2010
I have two tables 1) tblRequests and 2.) tblMainTags. The Fields in tblRequests are Tag_Request_No, Employee_ID , Accepted, Accepted_Date, and Accepted_Time. The Fields in tblMaintags are Tag_Request_No, Tag_ID, Completed, ... The fields 'Accepted' and 'Completed' are set to YES/NO.
[Code]...
View 5 Replies
May 27, 2010
how to make a report which is from join tables!
View 13 Replies
Oct 4, 2011
customers
phonenumbers
customers_has_phonenumbers
customers -> detailed
customers.customer_id int(11) primary auto_increment
[Code] .....
How to get the data by customers.customer_id from phonenumbers?
View 1 Replies
Sep 26, 2010
I have a problem i need to join two table from different sql servers and as a test i have tried to get data from two table but i realizethe adapter can have only one connection statement as argument how do i get records from two table from diffrent database
Please see my code
Dim adapter As SqlDataAdapter
Dim ds As New DataSet
[code].....
View 6 Replies
Mar 11, 2010
Table 1 has fields: A, B, C, Table 2, there are fields: D, E, F, Table 3 has fields: G, H, I
Connect the condition that A = D and F = I
provide a look at Table A left join B, C of the SQL statement
View 1 Replies
Dec 21, 2011
How to Query multiple tables in a dataset to form single consolidated table without using any external database. I know this can be done using 'JOIN' in SQl.
Now my condition is I have multiple tables filled with data in a dataset and each table is interrelated with a common 'column'. For example ,
Table 1
ID
Name
1
[Code].....
Here I mentioned only two tables but in my case there are many tables. I need a generic answer for this dynamic query .
View 8 Replies