How to join datatables of a dataset? - c#

I have a dataset which have four datatables.
Table[0] contains columns such as: storedProcedure,DLLMethod,BLLMethod
Table[1] contains columns such as: DLLMethod,BLLMethod
Table[2] contains columns such as: UCName,BLLMethod
Table[3] contains columns such as: UCName,Function,Module
Table[4] contains columns such as: StoredProcedure,Function,Module
I want to join first four tables such that final table must contain StoredProcedure,Function and Module.Can anyone Please help me with C# coding?

LINQ to DataSet
dim query = from a in table1 join b in table2 on a.table2ID = b.ID select a,b
this can grow on many more tables by adding more joins
var query = from a in table1 join b in table2 on a.table2ID = b.ID select a,b;

Related

LINQ - How to get data from multiple tables

I have my Table1 that contains the following columns.
My Table2 contains
I am currently getting all the rows from Table1 that are in a date range as follows:
dbRowList = context.Table1.Where(x => x.dateTime > from && x.dateTime < to).ToList();
However, I would like to also get only the rows that, for the same idCycle in Table2 (=id of Table1), have the 'label' field with certain string content.
How to join it or query it using LINQ?
Table1 join Table2 on Table1.id = Table2.idCycle.
Filter for the date range for the records in Table1 and Label in Table2.
Select all columns (or specify the column that is required) from Table1.
var result = (from a in context.Table1
join b in context.Table2 on a.id equals b.idCycle
where (a.dateTime > from && a.dateTime < to)
and b.label = /* Value for Label to filter */
select a
)
.ToList();

Linq to entities, How to select all columns and also select custom columns?

I need to be able to do this query in linq
select t1.*, t2.column1, t2.column2
from Table1 as t1 inner join Table2 as t2
Where t2.x = true
the main table we want to get all his columns and some other columns from other tables. Can this be done in linq?
from element in t1
join otherElement in t2
on element.match equals otherElement.match
where t2.x == true
select new {
column1 = otherElement.column1,
column2 = otherElement.column2
//add all the elements of t1 here
}
A join clause takes two source sequences as input. The elements in each sequence must either be or contain a property that can be compared to a corresponding property in the other sequence.
https://msdn.microsoft.com/en-us/library/bb311040.aspx

How to combine and retrieve multiple columns from two tables in a Dataset

I have a DataSet with two tables connected by a reference (Loop_id)
Table1
Column1 Column2 Loop_id
1 ItemCode_AAA 6
2 ItemCode_BBB 8
Table2
Column1 Loop_id
2014-Sep-09 6
2014-Nov-09 8
How do I retrieve all the columns, except loop_id from both tables to single table.
The resulting table should appear like
T1_Column1 T1_Column2 T2_Column1
1 ItemCode_AAA 2014-Sep-09
2 ItemCode_BBB 2014-Nov-09
I am using Net Framework 4.5
Use Linq to DataSets:
To enumerate the table, call AsEnumerable.
DataTable table1 = ds.Tables["table1"];
DataTable table2 = ds.Tables["table2"];
var records = (from t1 in table1.AsEnumerable()
join t2 in table2.AsEnumerable()
on t1.Field<int>("Loop_id") equals t2.Field<int>("Loop_id")
select new {T1_Column1 = t1.Field<string>("Column1"),
T2_Column2 = t2.Field<string>("Column2"),
T2_Column1 = t2.Field<string>("Column1")});
Use View
Or you can use join
SELECT T1.Column1 AS T1_Col , T1.Column2 AS T1_Col , T2.Column1 AS T2_Col
FROM dbo.Table1 AS T1 RIGHT OUTER JOIN
dbo.Table2 AS T2 ON dbo.T1.Loop_id = dbo.T2.Loop_id

How to compare/match values in sql server in two different tables

I need to have two tables.
First table (tblStudent) has fields student_id,firstname,lastname,class,rollno.
Second table(feeType) has fields class,admissionFee, tuitionFee etc.
I want to show the different fee for each class.
Like class 5 pays 600 as tuition fee where as class 6 will pay 700.
Eg. If class is 5 in first table then only the class 5 row in feeType should be shown ? How do I design tables as such? How do I join two tables? I will be using sql server 2008 and c# as frontend. Thanks from a novice.
This is the simplest type of INNER JOIN:
SELECT *
FROM tblStudent AS s
INNER JOIN feeType as f ON s.class = f.class
Your INNER JOIN would only return matching records,for non-matching records you could use a FULL JOIN and WHERE criteria to check.
SELECT *
FROM tblStudent AS s
INNER JOIN feeType as f ON s.class = f.class where s.class=null;
Visit here
This will list all the class names and their fees.
SELECT
s.class,
f.admissionFee as admission_fee,
f.tuitionFee as tuition_fee
FROM
tblStudent as s
JOIN feeType as f ON s.class = f.class
GROUP BY
s.class;
I hope this will help you to get the exact result set.

i want two columns value in different table in mysql and each table i apply different condition

i have two table. table1 and table2.each table is having three columns.
each table columnsname is different.i apply the query for table .i want one columns value for table1
and i want two columns value for table2.i want to write one query for each table and apply different condition for each table.but ,i want to result in only one query
You can concatenate to results with union
select col1 as A, col2 as B, col3 as C from table1 where col1 = 'foo'
union all
select colA as A, colB as B, colC as C from table2 where colB = 'bar'
use joins , it can be retrived
select table1.colname,table2.colname from table1, table2
where table2.colname=table2.colname.
SELECT tbl1.columnName,tbl2.columnName1,tbl2.columnName2
FROM table1 tbl1
OUTER APPLY
(
SELECT columnName1,columnName2 FROM table2 WHERE table2.field_fk = tbl1.filedId
)tbl2

Categories

Resources