C# - LINQ To SQL Join Two Tables To Select Parent Table Twice Based On Two Different Columns From Child Table?

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


ADVERTISEMENT

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

C# - LINQ To SQL Join 3 Tables And Select Multiple Columns And Also Using Sum

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

Creating DataRelation With Parent And Child Tables And Generating Xml With Subnodes For Child Table?

May 21, 2012

Dim obj_DataTable As New System.Data.DataTable("Category")
Dim obj_DataSet As New DataSet()
'Declaring the array of DataColum to hold the Primary Key Columns

[code].....

View 1 Replies

Refresh Table Adapter When I Have A Child Table Attached With Parent Table?

Dec 21, 2011

I have a data table whose one column is related to a column of another table. I have a listbox in a form which shows a column (which is sorted by another column value by ORDER clause) of the parent table and other columns are in textboxes. The child table is represented by a datagrid. When I add a new item in parent table and click save, the newly created item is listed at the bottom of the listbox violating my ORDER clause. When I wrote some codes to fill data again after updating, it shows an error message[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

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

LINQ Query - Join Tables On Nullable Columns

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

Show Parent Table And Child Table In Datagridview?

Jun 18, 2009

in DataSet ds I have "tbl1" as parent table and "tbl2" as childtablehow to show both table in datagridview if column id in "tbl1" related to column headerId in "tbl2" ?

View 1 Replies

Viewing Child Table With Parent Table In Datagrid?

Jun 17, 2009

I have a SQL Data Base. I have created a relation between these tables. Problem is when i drag and drop it on Data grid i get only Parent table columns but i want to sea them both like this.

- Parent Table Row
- Child Row 1
- Child Row 2

[code].....

View 6 Replies

Aggregating Parent/child Tables Using Linq To SQL?

Sep 18, 2009

I'm having a nightmare with LINQ. I've got a table of Projects, and each project has many InvoiceHeaders. The Invoice header had a field AmountNet - the value of the invoice.

I want to retrive, for arguments sake, all of the fields from Projects, with the count of invoice headers and the sum of AmountNet. I.e. list of my projects, how many invoices I've raised and the value.

Doesn't sound hard does it.... well, I've just spent the best part of two hours charging through Google and SO to find a solution.

[Code]...

View 1 Replies

How To Join 2 Tables In Combination With Relational Table

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

C# - LINQ - Select All In Parent-child Heirarchy

Mar 12, 2012

I was wondering if there is a neat way do to this, that DOESN'T use any kind of while loop or similar, preferably that would run against Linq to Entities as a single SQL round-trip, and also against Linq To Objects. I have an entity - Forum - that has a parent-child relationship going on. That is, a Forum may (or in the case of the top level, may not) have a ParentForum, and may have many ChildForums. A Forum then contains many Posts.

What I'm after here is a way to get all the Posts from a tree of Forums - i.e. the Forum in question, and all it's children, grandchildren etc. I don't know in advance how many sub-levels the Forum in question may have. (Note - I know this example isn't necessarily a valuble use case, but the Forum object model one is one that is familar to most people, and so serves as a generic and accessible premise rather than my actual domain model.)

View 2 Replies

SQL Statements For MS ACCESS - Insert A New Record Into The Existing Table And Join Tables?

Apr 18, 2009

i am using ASP.NET with VB.NET to connect to a MS Access database. how can i make the sql statement to insert a new record into the existing table and join tables?

View 1 Replies

Multi - Table Join Query In ASP.NET To SQL Server Via LINQ?

Jan 29, 2012

I'm working on an ASP.NET 4.0 site, which I inherited ownership of. It has a number of existing LINQ datasources pointing to individual tables. For example, to the Patient Encounter Summary table in SQL Server. The problem is in displaying the data there. It's a normalized database, so that table contains (for example) the provider ID, rather than the provider name.

It's simple enough to join the Patient Counter Summary to the Providers table (in SQL)... but how does one do that in ASP.NET? I'm not sure of the correct nomenclature, but the 'mid layer' is VB.

[Code]...

View 3 Replies

LINQ Left Outer Join W/ Date Range Restriction On Right Table?

Feb 4, 2011

I have two tables, a LP_task table and an lp_Update table. I want a complete list of tasks and only updates that were posted during a specific date range. LINQ doesn't seem to support any other join criteria but 'equals'or each task. I want all tasks (left table) even if they don't have an update. (Left Outer Join)

Dim TasksData = (From t In db.LP_Tasks _
Group Join up In db.LP_Updates On t.ID Equals up.TaskID Into upds = Group _
From u In upds.DefaultIfEmpty _

[code]......

View 1 Replies

Combining Columns Of Two Tables In One Table?

Dec 16, 2009

I am working in vb express 2008.I want to combine two tables to show all the columns of two tables in one table. I tried following query:

"Select de.Description, SUM(de.Quantity) AS Quantity, SUM(de.Amount) AS Amount " & _
"FROM DailyExpenses de " & _
"WHERE de.EDate Between @START and @END " & _

[code]....

And i want to combine two tables to display the following result into a Listview through a SQL query:

Description Quantity AmountDescription Quantity Amount
Mineral Water 12 26 Coca Cola 2 25
Rice 3 215 Sprite 3 15
Beaf 4 150 Beaf 4 50

View 6 Replies

VS 2008 Updating Child Table Before Parent?

Jan 11, 2011

I have a situation where a database table is updated with records inside a loop. Every record in this table has a parent child relationship with another table. This child table is also updated subsequently inside the same loop.I am creating the datatable for Parent table and adding records inside the loop, but committing the records in the database (using SqlCommandBuilder) outside the loop only once.

Inside the loop I am also calling the function for updating records in the child table. Because the adaptor and datable objects are created inside the child table function, I need to commit records in child table inside the function only. So this happens before commit of parent table. Because there is a PK-FK constraint in the database, an error is generated.

Simplest solution would be to bring all code inside one function. But I don't want to do that. Besides this parent child relations can go upto any levels.

[Code]...

View 8 Replies

SQL Query With Three Tables And Sum Of The Product Of 2 Columns Of One Table

Sep 10, 2011

I am having 3 tables in my DB Challans, Challan_Details & Customers. They are as follows

Table Challans:

Customer_ID (Related with Customer_ID in Customers Table) Challan_ID (Key) Challan_VchNo (Voucher No.) Challan_Date Invoice_ID
Table Challan_Details

[Code]....

View 3 Replies

Updating A Child Table Using Access Tables?

Mar 13, 2009

I am getting a System.Data.InvalidConstraintException" ForeignKeyConstraint CompanyPasswords requires the child key values(-1) to exist in the parent Table. When saving updates to the parent and child tables.My parent table consists of 3 columnsCompanyID (auto increment, pk)Company NameURLMy Child table consists of 4 columns PasswordID (auto increment, pk)CompanyID (fk)User NamePasswordHere is all the code on the form

[Code].....

View 5 Replies

Loop Through Child - Bind Textbox To Field Of Parent Table

Mar 22, 2010

First I have 2 tables, for example Customer and Orders, and in the database there is a foreign key relation between both. For clarity Orders have a foreign key field to the primary key of Customers, thus Customer is the parent and Orders is the child table of the relation. My application is a Windows Form application, and I add a dataset to the project, in which I drop the 2 tables, and the relations between the 2 tables is automatic added. Now I will om a form loop through the Orders table (witch is the Child table) and show in a textbox some fields of the Orders table and also the Name field of the related record of the customers table. Regarding binding a textbox fo a Orders field I do it this way,

Dim myDs As New DataSet1
Dim myAdpCustomer As New DataSet1TableAdapters.CustomerTableAdapter
Dim myAdpOrders As New DataSet1TableAdapters.OrdersTableAdapter
myAdpCustomer.Fill(myDs.Customers)
myAdpOrders.Fill(myDs.Orders)
Dim myBindingSource As New BindingSource
myBindingSource.DataSource = myDs.Orders
myBindingNavigator.BindingSource = myBindingSource
TextBox1.DataBindings.Add("Text", myBindingSource, myDs.Orders.OrderIDColumn.ColumnName)

But now I can't find how to bind a textbox to a field of the Customers table witch is the Parent table of the relation.

View 1 Replies

Showing Data From Child Table By Clicking On Parent Record?

Apr 4, 2009

Instead of connecting to database,I create datacolumns of two data tables.assign foregin key constraints and primary keys to the columns.Add these datatables to dataset and create relationship between them.Now by using RowFilter property,how to display the child table in a datagrid by clicking on parent record in another datagrid?Accept and Award Points Accept as Solution

View 2 Replies

Ensure That The Currently Selected Row ID In Parent Table Automatically Appears In Child Form?

Feb 26, 2009

How can I ensure that the currently selected row ID in Parent Table automatically appears in my child form when I create a new row in the Child Form.I am using VB 2008 Express I have a Stored Procedure for the chiled table called "Files" can I do it from there:-

ALTER PROCEDURE dbo.CreateFile
(
@FileName varchar(50),
@DateCreated varchar(50),

[code]....

View 1 Replies

Postgresql - Populate Tree View For Parent Child Relation In Same Table?

May 1, 2011

The database strutcure is:

Id Name ParentId
1 File NULL
2 Open 1
3 Save 1

[Code]....

I am using the database PostgreSQL.

View 1 Replies

Require Assistance Making A Menu From A Single Parent/Child Table?

Oct 25, 2010

I have a single table that holds my Categories

CatID, CatName, ParentID

I have often used this layout to show Root Categories where the ParentID IS NULL. I would like to be able to create a vertical Menu (using a repeater and an <UL><LI> that displays the Root Categories and ideally, a collapse panel that would expand with the subcategories. Given the above Table format, it is also easily possible to make the hierarchy unlimited.

how to make a single dataset that understands the Child relationship.

View 1 Replies

Way To Create Empty Data Table / Update Its Contents Based On Columns Not Add Data Based On Addition Of New Rows

Apr 6, 2011

I am creating a project in VB.NET in which one of the reports require that the name of employees should be displayed as column names and whatever work they have done for a stated period should appear in rows below that particular column.Now as it is clear, the columns will have to be added at runtime. Am using an ODBC Data source to populate the grid. Also since a loop will have to be done to find out the work done by employees individually, so the number of rows under one column might be less or more than the rows in the next column.Is there a way to create an empty data table and then update its contents based on columns and not add data based on addition of new rows.

View 1 Replies

Dynamic Sql Table Creation - Create Sql Tables Based On User Need

Apr 20, 2012

i need to create sql tables based on user needs lets say a guy needs a table of 4 rows 5 columns other user needs 3 rows 3 columns each with different types so how is the best way for letting them choose like they write in a text box ( this will be the value of rows)4 and another text bvox the count of columns and i have those 2( rows and columns needed) numbers i create the table as is.based on those. by default i create type as varchar max then later on we must fill the table using datagrid.

View 14 Replies

Add Custom Columns To A Table That LINQ To SQL Can Translate To SQL

Aug 26, 2009

I have a table that contains procedure codes among other data (let's call it "MyData"). I have another table that contains valid procedure codes, their descriptions, and the dates on which those codes are valid. Every time I want to report on MyData and include the procedure description, I have to do a lookup similar to this:

From m in dc.MyDatas _
Join p in dc.Procedures On m.proc_code Equals p.proc_code _
Where p.start_date <= m.event_date _

[Code]....

Is there a way to turn a complex lookup (i.e. a non-trivial join) like this into something SQL can recognize so that I can define it in one place and just reference the description as if it were a field in MyData? So far the only thing I can think of is to create a SQL view on MyData that does the linking and bring that into my data context, but I'd like to try to avoid that.

View 2 Replies

Creating Xml File From Master Detail Access Tables With Two Key Columns In Each Table In Vb 2005

Sep 10, 2009

I'm getting this error 'column' argument cannot be null. Parameter name: column at the bold character instruction. Also I would like to know how to relate two tables with two key columns in each table

Dim connection As New OleDb.OleDbConnection(strTextoConn)
Dim EnPartesdataSet As DataSet = New DataSet("Enpartes")
Dim strsql As String

[Code].....

View 1 Replies

SQL SELECT With Multiple Tables To Datatable/DataView Get Table Names

Feb 21, 2011

For my app users can enter a SELECT statement themselves. This can be any select statement at all, it will be mainly used for creating a quick view e.g. SELECT * from table1 LEFT OUTER JOIN table2 on table1.id = table2.id.

Whatever this select statement maybe I load it into a datatable (after checking for no nasty stuff of course) in vb.net. I want to know the table names so I can query the database again for validation purposes on things like datatypes etc.

da.SelectCommand = New SqlCommand(sqlStatement, tempDB.Connection)
da.MissingSchemaAction = MissingSchemaAction.AddWithKey
dt = New DataTable
da.Fill(dt)

View 3 Replies







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