Linq query to join 4 tables - c#

I have a table called deliveries with columns id,shiftid, occurrence,delfee). I have three other tables delcash,delcc,delsplit. Del cash has columns (id,cost,paid), delcc has columns (id,cccost,ccpaid,cashtip), delsplit has columns(id,cashcost,cashpaid,cccost,ccpaid). The id links the delivery entries to one of the three other tables based on the type of payment. I am trying to get a list where the tables are joined together in a way so i can iterate through a list and get a table in a view that shows each delivery in order of occurrence as well as the tip and delivery fee of each delivery. Any idea if it is possible to do this? Or should I just use SQL?
SELECT deliveries.occurrence, deliveries.delfee,delcash.cost,delcash.paid,delcc.cost,delcc.paid,delcc.cashtip,
delsplit.cashcost,delsplit.cashpaid,delsplit.cccost,delsplit.ccpaid
FROM deliveries
LEFT JOIN delcash
ON deliveries.id = delcash.id
LEFT JOIN delcc
ON deliveries.id = delcc.id
LEFT JOIN delsplit
ON deliveries.id = delsplit.id

You can make a new class and let the three cost tables inherit from this class then you can apply the Table per Concrete Type strategy for mapping the tables.
see:
http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.aspx
OR redesign your database and apply Table per type strategy.

Try something like this:
var joinedDel = from delivery in tblDeliveries
join delC in tblDelCash on delivery.id equals delC.id
join delCC in tblDelCreditCard on delivery.id equals delCC.id
join delSplit in tblDelSplit on delivery.id equals delSplit.id
select new {
//Then create a new instance of Anonymous object where you will set all values of its properties
//For example, you can set ShiftID = delivery.ShiftID
};
I will also suggest to normalize first your tables(see this link for your reference) . This will give you more understanding on how will you join those tables properly.

Related

LINQ to DataTable Join Query using OR condition

I have two DataTables with different data,common columns in both are quantity and Itemcode. I want to join the two tables together where two fields, (DataTable1.itemcode=DataTable2.itemcode) AND (DataTable1.quantity=DataTable2.quantity) OR ((DataTable1.quantity+1)=DataTable2.quantity) OR ((DataTable1.quantity+2)=DataTable2.quantity) . How would I write this in LINQ?
if item code is same and quantity is equal,exceeds by 1 or 2 then I should get joined result.
I tried using anonymous objects but that is for AND condition and OR condition was not fulfilled,so only itemcode and any one of these conditions was achieved at a time?
Please suggest me the correct way to do it?
You have to use the following syntax to access fields of the dataset:
[DataTable Name].Field<string>(indexNo or ColumnName)
So you can write the following linq to put a join between two data tables:
from t1 in dataTable.AsEnumerable()
//join between two tables
join t2 in dataTable2.AsEnumerable() on t1.Field<string>(0) equals t2.Field<string>(0)
//where conditions
where t1.Field<string>(0) == [value]
//select clause
select t1.Field<string>(colmNo)
Also you can convert the two data tables to IEnumerable by creating extension method.

Using the SQL "IsNull()" command in NHibernate criteria in C#

I need to be able to use with NHibernate criteria, the SQL's IsNull() function in C#.NET. I don't need to use it with LINQ.
Meaning that Table1 has the following columns:
Name | Description
Table2 has the following columns:
OriginalDescription | TranslatedDescription
And Table1.Description = Table2.OriginalDescription.
How would I write the following SQL statement with NHibernate criteria:
SELECT Table1.Model, IsNull(Table2.TranslatedDescription, Table1.Description)
FROM Table1
LEFT JOIN Table2 ON Table2.OriginalDescription = Table1.Description
The SQL statement above will give me the Names, and TranslatedDescriptions if the TranslatedDescriptions exist, otherwise it will return the Descriptions, for the records.
There cannot be duplicates of OriginalDescription in the Table2.
The solution of the ISNULL could be expressed like this:
// here is the criteria of the "Entity1" and the join to the "Entity2"
var criteria = session.CreateCriteria("Entity1", "table1");
criteria.CreateAlias("Entity2", "table2");
// here we drive the SELECT clause
criteria.SetProjection(
Projections.ProjectionList()
.Add(Projections.Property("Model"))
.Add(Projections.SqlFunction("COALESCE", NHibernateUtil.String
, Projections.Property("table2.TranslatedDescription")
, Projections.Property("table1.Description")
))
);
// just a list of object arrays
var list = criteria.List<object[]>();
So, what we do here, is a call of the SqlFunction. In this case one of the out-of-the-box mapped in many Dialects coming with NHibernate (but we can even extend the dialect with custom ones, an example how to: Nhibernate count distinct (based on multiple columns))
Must note, that the JOIN Clause is coming from the mapping. So this Table2.OriginalDescription = Table1.Description must come from a mapped relation many-to-one

SQL 2 INNER JOINS with 3 tables

I've a question about a SQL query.. I'm building a prototype webshop in ASP.NET Visual Studio. Now I'm looking for a solution to view my products. I've build a database in MS Access, it consists of multiple tables.
The tables which are important for my question are:
Product
Productfoto
Foto
Below you'll see the relations between the tables
For me it is important to get three datatypes: Product title, price and image.
The product title, and the price are in the Product table. The images are in the Foto table.
Because a product can have more than one picture, there is a N - M relation between them. So I've to split it up, I did it in the Productfoto table.
So the connection between them is:
product.artikelnummer -> productfoto.artikelnummer
productfoto.foto_id -> foto.foto_id
Then I can read the filename (in the database: foto.bestandnaam)
I've created the first inner join, and tested it in Access, this works:
SELECT titel, prijs, foto_id
FROM Product
INNER JOIN Productfoto
ON product.artikelnummer = productfoto.artikelnummer
But I need another INNER JOIN, how could I create that? I guess something like this (this one will give me an error)
SELECT titel, prijs, bestandnaam
FROM Product
(( INNER JOIN Productfoto ON product.artikelnummer = productfoto.artikkelnummer )
INNER JOIN foto ON productfoto.foto_id = foto.foto_id)
Can anyone help me?
something like this should work:
SELECT Product.titel, Product.prijs, Foto.bestandnaam FROM Product INNER JOIN
(Foto INNER JOIN Productfoto ON Foto.[foto_id] = Productfoto.[foto_id]) ON
Product.[artikelnummer] = Productfoto.[artikelnummer];
One thing about the use of linking tables
The ProductFoto table allows for N-M relations between Product and Foto indeed. Is this what you really want/need? In other words, can one Foto belong to more than one Product? If not, put the Product_Id on the Foto table. If so,...
...let's discuss JOIN.
Say we have two tables, A and B. doing a
SELECT * FROM A, B
will give you all permutations of A's rows with B's rows. We could limit the resultset by adding a WHERE clause, like WHERE A.a='lekker hoor!', or a way cooler WHERE A.id=B.a_id. Which actually starts to look like a JOIN result!
Lets do a proper JOIN then:
SELECT * FROM A JOIN B ON A.id=B.a_id
JOINs actually come in LEFT OUTER, RIGHT OUTER and FULL INNER or FULL OUTER joins.
A LEFT JOIN (use of OUTER is optional) will contain all records in the left (first) table, even if there is no corresponding records(s) in the right (second) table.
A RIGHT JOIN obviously works the same way, but mirrored.
With a FULL OUTER JOIN both tables are optional (not quite the same as SELECT * FROM A, B though!).
A FULL INNER needs matching records from both tables (this is the default).
When you do want to do more than one JOIN, say
SELECT * FROM
A
JOIN B ON A.id=B.a_id
JOIN C ON B.id=C.b_id
You can think of the extra JOIN as joining on an intermediate table, formed by joining A and B, especially when mixing some LEFT/RIGHT/INNER/OUTER JOINs.
As to your question
Use something along the lines of
SELECT TOP (1) titel, prijs, bestandnaam
FROM
( -- this bracket is MS Access specific (and awkward)
Product
INNER JOIN Productfoto ON product.artikelnummer = productfoto.artikelnummer
) -- this one too
INNER JOIN foto ON productfoto.foto_id = foto.foto_id
to satisfy MS Access, use brackets around first two tables, see Ms-Access: Join 3 Tables
Normally no brackets required (you'll get to use them like this when you discover sexy sub-selects, for which the rule is: only use them if there is no alternative).
Because there are multiple matches in your ProductFoto table, there are multiple matches in your result. Use TOP 1 (or LIMIT 1, depending on your DB) to 'fix' this.
Veel success, en doe jezelf een plezier en switch to English!

Select all columns after JOIN in LINQ

I have two tables, Table1 and Table2. I want to perform, say, a left outer join:
var myOutput = from object1 in Table1
join object2 in Table2
on object1.Property1 equals object2.Property2 into Table3
from output in Table3.DefaultIfEmpty()
select new
{
object1.Property1,
object1.Property2,
//...
output.Property3,
output.Property4,
//...
};
As you can notice, I want to select all the properties of both objects from the resulting table (the enumerables considered while joining contain the objects of certain types - these are different for both relations). Of course, I can select the properties in the anonymous select, as shown in the example.
My question is how to avoid specifying all the properties manually? I would like to have something like SELECT * FROM TABLE3, where TABLE3 is a resulting relation (after joining TABLE1 and TABLE2).
Thanks in advance for the clues.
You have to specify each manually if you want to project into a flattened type. Your other option is to just have your combined type contain both objects, and the objects will naturally bring along their properties.
select new
{
Object1 = object1,
Object2 = output
};
And you would work with it like myObj.Object1.Property1, myObj.Object2.Property4, etc.
One final option that still involves some manual work is to define an appropriate type and have a constructor or a builder method that does the work of segmenting out your object properties into a flattened type. You still perform the manual mapping, but you isolate it from your query logic.
select new CombinedType(object1, output);
//or
select builder.GetCombinedType(object1, output);

Querying a join table using LINQ

Okay this could very likely be a silly question. I am using Entity Framework Code First. I have two classes, User and Event, that have a Many-To-Many relationship. When EF generates my database tables, it creates a join table, which I call Users_Events. This table has two columns, User_ID and Event_ID. Everything is fine so far.
I want to pull an Event from my database and serialize it to JSON. This also works perfectly except I cannot pull an Event's Users because this would create a circular reference. What I want to do here is query my join table and get all the User_IDs that have an associated Event_ID equal to the ID of the Event I am serializing.
How can I do this?
I don't know exactly what you want to end up with in your JSON, but I suspect you want to select into a new anonymous type and serialize that instead. Something along these lines maybe:
from e in myContext.Events
where e.ID = 123
select new {
Event = e,
UserIDs = (from u in e.Users select u.ID)
}

Categories

Resources