I have an excel template for uploading records to database and it has multiple sheets.
I am able to join the sheets' data on upload using LINQ and this is my code:
var query = from component in ds.Tables[0].AsEnumerable()
join componenthazardous in ds.Tables[1].AsEnumerable()
on component.Field<string>("PartNumber")
equals componenthazardous.Field<string>("PartNumber")
join manufacturers in ds.Tables[2].AsEnumerable()
on component.Field<string>("PartNumber")
equals manufacturers.Field<string>("PartNumber")
join vendorinfo in ds.Tables[3].AsEnumerable()
on component.Field<string>("PartNumber")
equals vendorinfo.Field<string>("PartNumber")
join crossreference in ds.Tables[4].AsEnumerable()
on component.Field<string>("PartNumber")
equals crossreference.Field<string>("PartNumber")
select new {
PartNumber = component["PartNumber"].ToString(),
Description = component["Description"].ToString(),
Hazardous = componenthazardous["Hazardous"].ToString(),
M_MfgrCode = manufacturers["MfgrCode"].ToString(),
M_MfgrName = manufacturers["MfgrName"].ToString(),
M_Status = manufacturers["Status"].ToString(),
M_AsOf = manufacturers["AsOf"].ToString(),
M_Remarks = manufacturers["Remarks"].ToString()
};
Now, my problem is that my boss suddenly changed the requirements. On excel template, the key I used to join the tables are duplicated. Its uniqueness will be based on the combination of PartNumber and MfgrName. How can I join the tables using this combination as key if only one sheet has these rowcolumns?
My idea is that I'll use row number as my key for joining since the partnumber column on other sheets is referenced from the first sheet/datatable but I don't know how. Any idea or any other way to solve this?
Thanks.
Related
I need to join the two tables of a DataGrid, but I only want the values with the same id (idviagem is pk in table idviagem and is fk in table idpassageiro)
I don't know how to do the query, in that moment I only take the table tbpassageiro on the grid, and I want to join them on DataGrid when the keys are equals
using (checkinEntities1 db = new checkinEntities1())
{
var qcheckin = (from c in db.tbpassageiro
join g in db.tbviagem on c.idviagem equals g.idviagem
where c.idviagem == g.idviagem
select c).ToList();
gridpass.ItemsSource = qcheckin;
}
The binding I know 100% is correct (some values from table passageiro and the other Biding values from table tbviagem)
This what I want to do:
If you want columns from both tables, then you have to create a view model for the columns which you want from both tables.
using (checkinEntities1 db = new checkinEntities1())
{
var qcheckin = (from c in db.tbpassageiro
join g in db.tbviagem on c.idviagem equals g.idviagem
where c.idviagem == g.idviagem
select new viewModelName()
{
//get the column values here like
Hora = c.Hora,
Partida = g.Partida
}).ToList();
gridpass.ItemsSource = qcheckin;
}
Hope this will give you the answer you are looking for.
I am learning ASP.net and I have come to the point that I want to insert, update, delete records in a database.
Currently I am trying to read out values out of 2 tables using "join" but when I display the results in a grid the Foreign Key values are still like : 2, 1, 2,... Instead I want them to be to coresponding words.
This is the current query I am using:
from p in dc.Personeels join a in dc.Afdelingens on p.fk_personeel_afdeling equals a.pk_afdeling_id select p
Does anyone know what I am doing wrong?
Try this query
var query = from p in dc.Personeels
join a in dc.Afdelingens on p.fk_personeel_afdeling equals a.pk_afdeling_id
select new
{
id = p.id, // your id from table dc.Personeels
name = a.name // Name from table dc.Afdelingens
} into x
select x;
I have a LINQ query (using with EF)
Basically I want to add a column in Select results based on value of another column.
I have PaymentDate column in DB table but not Paid column. If there is null in PaymentDate column it also shows payment is false and if it has some date in it means paid is true.
Here is my query, please guide me how to do that.
var selectedResults=
from InvoiceSet in Invoices
join BookedAreasSet in BookedAreas
on InvoiceSet.InvoiceID equals BookedAreasSet.InvoiceID
join AreaSet in Areas on BookedAreasSet.AreaID equals AreaSet.AreaID
select new {InvoiceSet.InvoiceNumber,InvoiceSet.Amount,InvoiceSet.TotalDiscount,InvoiceSet.GST, InvoiceSet.PaymentDate,InvoiceSet.ShoppingCentreID,BookedAreasSet.BookedAreaID,
AreaSet.Name,Here I want to add calculated value column based on InvoiceSet.PaymentDate value}
I think you should be able to do something like this
var selectedResults=
from InvoiceSet in Invoices
join BookedAreasSet in BookedAreas
on InvoiceSet.InvoiceID equals BookedAreasSet.InvoiceID
join AreaSet in Areas on BookedAreasSet.AreaID equals AreaSet.AreaID
select new { InvoiceSet.InvoiceNumber,InvoiceSet.Amount,InvoiceSet.TotalDiscount,InvoiceSet.GST,
InvoiceSet.PaymentDate,InvoiceSet.ShoppingCentreID,BookedAreasSet.BookedAreaID,
AreaSet.Name,Paid = (InvoiceSet.PaymentDate == null) }
I have a list of SiteUsers in one table and another table has columns with different types of owners (ID) for the record. For example, the SiteUserID in the SiteUsers table will be used for the SalesRepID, the StaffingManagerID, and RecruiterID in the Fill table. Of course, the SiteUserID is different for each of the values in the Fill table.
I'd like to return the name of the SiteUser for each ID column in the Fill Table.
How do I properly construct a JOIN statement to do this?
I'm guessing this is done through INNER JOIN, but I'm not sure.
My current select statement already has an INNER JOIN as I'm pulling the name of the FillType from another table. I'm using this in an asp.net application.
I'm not sure if this is even possible. Any help is appreciated.
Since each of the IDs in the Fills table allows null, you probably want to LEFT JOIN to the SiteUsers table like so:
SELECT f.FillID, s1.SiteUserLastName 'SalesRep', s2.SiteUserLastName 'StaffingManager', s3.SiteUserLastName 'Recruiter'
FROM Fills f
LEFT JOIN SiteUsers s1 on f.SalesRepID = s1.SiteUserID
LEFT JOIN SiteUsers s2 on f.StaffingManagerID = s2.SiteUserID
LEFT JOIN SiteUsers s3 on f.RecruiterID = s3.SiteUserID
You can always UNPIVOT the results like so:
SELECT
DISTINCT
unpvt.FillID
,unpvt.RepID
,unpvt.RepType
,s.SiteUserFirstName
,s.SiteUserLastName
FROM
(SELECT
FillID
,SalesRepID
,StaffingManagerID
,RecruiterID
FROM Fills
) f
UNPIVOT
(RepID FOR RepType IN
(SalesRepID, StaffingManagerID,RecruiterID)
) AS unpvt
JOIN SiteUsers AS s on unpvt.RepID = s.SiteUserID`
Obviously you can play with exact output (such as substituting the RepType for a different value with a CASE statement or whatnot.
My question is: why the piss-poor design? Instead of having three IDs in the Fills table, you should have a junction table between SiteUsers and Fills to allow many-to-many relationships. IF it were designed with a junction table, you'd never have had to ask this question.
You will have to join the Fill table with the SiteUsers table multiple times, one for each xxxID column in the Fills for which you want the SiteUser name and combine the results using an union as below:
select a.SiteUserId, a.SiteUserFirstName, a.SiteUserLastName
from dbo.SiteUsers a
inner join dbo.Fills b on b.SalesRepId = a.SiteUserId
UNION
select a.SiteUserId, a.SiteUserFirstName, a.SiteUserLastName
from dbo.SiteUsers a
inner join dbo.Fills b on b.StaffingManagerId = a.SiteUserId
UNION
select a.SiteUserId, a.SiteUserFirstName, a.SiteUserLastName
from dbo.SiteUsers a
inner join dbo.Fills b on b.RecruiterId = a.SiteUserId
I need to combine two or more table into one object by using C# 4,0... I wrote a class for a table which included simple select selectbyid insert update and update.... it works fine for single table... by the way I have two attribute which specifies table name column name and primarykey... by using all these I can create my simple methods but I need to select and update more table in one object or method... what should I do or what would you suggest about it...
Example:
users and customer table I have foreign keys which defined...
If you`re using linq to sql, you can join the other tables like
var q =
from s in db.Suppliers
join c in db.Customers on s.City equals c.City
select new {
Supplier = s.CompanyName,
Customer = c.CompanyName,
City = c.City
};
as just copy & paste from a sample of MSDN LINQ to SQL: .NET Language-Integrated Query for Relational Data