I have two tables in sql and i want to retrieve and fill a datagridview using 4 column from first table and 1 column from second table. My problem is, i am doing some ordering for the data from first table and i couldn't figure out how to join second table's column data. MeasResults table is the first table and Moulds is the second table.
Here is how i get MeasResults table data;
using (LinqDataClassesDataContext dataContext = new
LinqDataClassesDataContext())
{
var query = from x in dataContext.MeasResults
where x.MoldID == cBMeasDBMID.SelectedValue.ToString()
group x by x.MeasId into grp
select grp.OrderByDescending(x => x.MeasId).First();
var result = query.OrderByDescending(x => x.MeasId).Take(5);
daGridLastMeas.AutoGenerateColumns = false;
daGridLastMeas.Columns["MeasId"].DataPropertyName = "MeasId";
daGridLastMeas.Columns["Date"].DataPropertyName = "Date";
daGridLastMeas.Columns["Plane"].DataPropertyName = "Plane";
daGridLastMeas.Columns["Position"].DataPropertyName = "Postn";
daGridLastMeas.DataSource = result;
}
And i tried to implement join function to get HeatCnt value from second table(Moulds).
using (LinqDataClassesDataContext dataContext = new
LinqDataClassesDataContext())
{
var query = from x in dataContext.MeasResults
where x.MoldID == cBMeasDBMID.SelectedValue.ToString()
join y in dataContext.Moulds on x.MoldID equals y.MID
group x by x.MeasId into grp
select grp.OrderByDescending(x => x.MeasId).First();
var result = query.OrderByDescending(x => x.MeasId).Take(5);
daGridLastMeas.AutoGenerateColumns = false;
daGridLastMeas.Columns["MeasId"].DataPropertyName = "MeasId";
daGridLastMeas.Columns["Date"].DataPropertyName = "Date";
daGridLastMeas.Columns["Heat"].DataPropertyName = "HeatCnt";
daGridLastMeas.Columns["Plane"].DataPropertyName = "Plane";
daGridLastMeas.Columns["Position"].DataPropertyName = "Postn";
daGridLastMeas.DataSource = result;
}
I know my problem is at select part of the query but i couldn't find out how to add y.HeatCnt to select query.
How can i do this?
NOTE
I've already studied the following thread about joining two tables, but it is not quite in same arrangement with way of my code.
Retrieving data from multiple tables using linq-to-sql
EDIT
I am bit confused about the data you want. But you can play arround with below solution and get your expected results.
var query =
from i in dataContext.Moulds
let p = dataContext.MeasResults.Where(p2 => i.MID == p2.MoldID).FirstOrDefault()
where i.MID == cBMeasDBMID.SelectedValue.ToString()
orderby i.MeasId descending
select new
{
MeasId = i.MID,
Date = p.Date,
Heat = i.Heat,
Plane = p.Plane,
Position = p.Position
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.