i have got this query in sql server server that do what i want..
how can i convert it to linq
select t1.IDHardware,h.DescricaoHardware
from dbo.ProcessoHardware t1
INNER JOIN
(select t2.IDHardware, max(t2.IDProcessoHardware) as maxVisit
from dbo.ProcessoHardware t2
group by t2.IDHardware,t2.IDProcesso) v ON
v.maxVisit = t1.IDProcessoHardware JOIN dbo.Hardware h ON t1.IDHardware=h.IDHardware
where t1.Estado=1 AND IDProcesso=1
this is where i am now...but i am unable to figure it past this point..
var ProcHardware = (from procHardware in db.ProcessoHardwares
where procHardware.IDProcesso == IDProcesso
select new { procHardware.IDHardware, procHardware.IDProcessoHardware, procHardware.IDProcesso, procHardware.Estado } into x
group x by new { x.IDHardware, x.IDProcesso, x.IDProcessoHardware, x.Estado } into t
let Max = t.Max(g => g.IDProcessoHardware)
select new { IDHardware = t.Key.IDHardware, Estado = t.Key.Estado, t.Key.IDProcesso,IDProcessoHardware=t.Key.IDProcessoHardware,cMax=Max }).ToList().Where(t => t.Estado == 1 && t.IDProcesso == IDProcesso && t.IDProcessoHardware==Max).Select(c => new VMProcessoChooseHardware
{
IDHardware = c.IDHardware
});
i have got this table that relates the Table hardware with a table Process.. this table is called processHardware. this table is discribed by: IDProcessHardware IDProcess IDHardware State
the field state can have 3 states (1-Insert, 2-Remove,3-Substitute).. so i can i have this:
IDProcessHardware IDProcess IDHardware State
1 10 1 1
2 10 2 1
3 10 1 2
4 10 1 1
5 20 1 1
what i want to get is get the IDHardware that were inserted but not removed from process.
so by giving the IDProcess = 10 i want to get the hardware with the hardware ids 1 and 2..
IDProcessHardware IDProcess IDHardware State
1 10 1 1
2 10 2 1
3 10 1 2
4 20 1 1
in the table above by giving the IDProcess 10 , it should give me the Hardware ids 2.
Thanks in advance...
After a lot of trial and error, and a lot of search i found this link
http://jetmathew.wordpress.com/2014/01/21/select-latest-record-from-recordset-using-sql-and-linq/
the guy was trying something similar to what i want..
so i pick the linq query and transform it..
this is what i have now
var ProcHardware = (from a in db.ProcessoHardwares
group a by new { a.IDHardware, a.IDProcesso } into latest
join b in db.ProcessoHardwares on new { dt = latest.Max(itm => itm.IDProcessoHardware) } equals new { dt = b.IDProcessoHardware }
select new { ID = b.IDHardware, Estado=b.Estado,IDProcesso=b.IDProcesso }).ToList().Where(t => t.Estado == 1 && t.IDProcesso == IDProcesso ).Select(c => new VMProcessoChooseHardware
{
IDHardware = c.ID
});
it still need the rest the information discribing the hardware like the serial number , or the description.
i will post here the complete query..
Related
Please consider these two tables in my database:
Header:
Id Name
-------------------------------
1 London
2 Berlin
3 Paris
and Details:
Id HeaderId Amount YearMonth
--------------------------------------------------------------------
1 1 1000 2010-01
2 1 2000 2010-05
3 2 3000 2015-04
4 2 2700 2017-12
5 2 4500 2016-10
6 2 7000 2011-09
7 1 3000 2009-05
I want Header records with related Last Details record. For example:
HeaderId HeaderName Amount
----------------------------------------------------
1 London 2000
2 Berlin 2700
3 Paris Null
I wrote this query for Inner Join version (But I want Outer Join version):
from h in Header
join d in Details
on h.Id equals d.HeaderId
select new
{
HeaderId = h.Id,
HeaderName = h.Name,
Amount = (Details.Where(k=>k.HeaderId == h.Id).OrderBy(m=>m.YearMonth).LastOrDefault() == null ? null : Details.Where(k=>k.HeaderId == h.Id).OrderBy(m=>m.YearMonth).LastOrDefault().Amount,
}
and I got this error:
System.NotSupportedException: LINQ to Entities does not recognize the method 'Details.LastOrDefault()Details' method, and this method cannot be translated into a store expression.
How can I get above result?
thanks
This query should return desired result:
from h in Header
from d in Details.Where(d => d.HeaderId == h.Id)
.OrderByDescending(d => d.YearMonth)
.Take(1)
.DefaultIfEmpty()
select new
{
HeaderId = h.Id,
HeaderName = h.Name,
Amount = d.Amount
}
You should change your code as :
Amount = Details.Where(k=>k.HeaderId == h.Id).OrderByDescending(m => m.YearMonth).FirstOrDefault(o=>o.Amount);
This might seem like a stupid question but i really need help. I don't often post question but this time i really help.
I need to have a linq to sql query that groups multiple columns. But not just that, one of the columns have specific that also need to be grouped base on certain condition.
The Query i have is this one.
using (var donnée = new ClassDonnéeDataContext(mycontrng))
{
var don = from d in donnée.Reservations
where (d.Date_Livraison.Value.Date == startDate.Value.Date) && d.Sortie_Cuisine != "Oui" && d.Livraison != "Annulée" && (d.Reserv_Boutique == "Non" || d.Reserv_Boutique == null)
group d by new
{
Gateau = d.Gateau,
Heure = d.Heure_Livraison,
Nb_Part = d.Part,
} into grs
select new
{
Gateau = grs.Key.Gateau,
Heure = grs.Key.Heure,
Nombre = grs.Sum(x => x.Nombre),
Nb_Part = grs.Key.Nb_Part,
};
var order = from ord in don
orderby ord.Heure ascending
select ord;
dgv.DataSource = order;
}
The result i'm looking for is to have The columns "Heure_Livraison" to be grouped by specific critiria.
The result of the Query is as follow.
Gateau: Heure: Nombre: Nb_Part:
Foret Noire 10 2 6
Ganache 10 2 6
Foret Noire 11 2 6
Ganache 11 2 6
Ganache 12 1 6
Now i want to add all the Cake of the same name, same Nb_Part Between 10-12. So the result Will like
Gateau: Heure: Nombre: Nb_Part:
Foret Noire 10 4 6
Ganache 10 5 6
Please if anyone has a suggestion to this question, give it to me !!!``
I finally get to solve the problem by creating a separate column and specify the data to be stored in that column so that after when querying I just have to pick that column.
Thank you for comment !
I have following LinQ query
var CGTABLE = (from cg in DbContext.CGTABLE
join tcg in DbContext.TCGTABLE on new { cg.CGroupId } equals new { tcg.CGroupId }
where tcg.TId == TId
select new {
CGroupId = cg.CGroupId,
CGroupCode = cg.CGroupCode,
Description = cg.Description,
C = cg.C,
DisplayOrder = cg.DisplayOrder
}).ToList();
CGTABLE = CGTABLE.OrderBy(g => g.DisplayOrder).ThenBy(g => g.C.OrderBy(c => c.CCode)).ToList();
which runs fine, but it is not doing second orderby using ThenBy ThenBy(g => g.C.OrderBy(c => c.CCode) What am I missing?
Sample data for better understanding.
Data in Tables
2
1
2
4
3
1
4
5
2
1
3
3
1
Should output after both outer and inner list ordered by
1
1
2
3
4
2
1
2
4
5
3
1
3
But Currently it is showing
1
4
5
2
1
2
1
2
4
3
3
3
1
You didn't want to order the main list, you are looking for a way to order inner list inside of outer one, I think.
So below code will do it for you:
var CGTABLE = (
from cg in DbContext.CGTABLE
join tcg in DbContext.TCGTABLE on new { cg.CGroupId } equals new { tcg.CGroupId }
where tcg.TId == TId
select new {
CGroupId = cg.CGroupId,
CGroupCode = cg.CGroupCode,
Description = cg.Description,
C = cg.C.OrderBy(x => x.CCode),
DisplayOrder = cg.DisplayOrder
}).ToList();
CGTABLE = CGTABLE.OrderBy(g => g.DisplayOrder).ToList();
i want to show all the parentID as possible using linq for example:
if i select "submodule_idparent = 6" return all 5 and return 1. Because, to show 6 i need 5 and 1.
submodule_id, submodule_name, submodule_idparent
1 Articles null
2 Suppliers null
3 Adjustment 1
4 Presentations 1
5 Categories 1
6 Subcategories 5
7 Corridors 1
8 Cellars 1
9 Purchases 2
I'm try some like that with the next code (MySQL)
SELECT DISTINCT(submodule_id) FROM users_privileges LEFT JOIN modules_options USING(moduleoption_id) WHERE users_privileges.user_id = 1
but is not recursive.
Thanks in advance (y).
Try this:
var query=GetAll(6);
public IEnumerable<users_privileges> GetAll(int submodule_id)
{
var query = from c in db.users_privileges
where c.submodule_id == submodule_id
select c;
return query.ToList().Concat(query.ToList().SelectMany(t => GetAll(t.submodule_idparent)));
}
http://blog.csdn.net/q107770540/article/details/7708418
i have 1 employee leave table which contain records like this:
empid leave_type Leave_from Leave_to leave_taken status
1 annual leave 3-3-2014 4-3-2014 2 approved
1 annual leave 5-3-2014 6-3-2014 2 approved
1 sick leave 10-3-2014 11-3-2014 2 approved
1 sick leave 12-3-2014 13-3-2014 2 approved
1 Casual leave 19-3-2014 20-3-2014 2 approved
1 Casual leave 22-3-2014 23-3-2014 2 approved
now i want to calculate total leave taken by particular employee by leave type of march month only whose status is approved like as shown in below:
leave_type leave_taken
annual leave 4
sick leave 4
casual leave 4
i have written query like this:
var data = (from r in context.emp_leaves
where r.Emp_id == empid && r.status == "Approved"
group r by new
{
r.Emp_id,r.leave_type
} into g
select new
{
leave_taken = g.Sum(x => x.leave_taken)
});
but it is showing wrong output.
can anyone provide me linq query for this???
Since you are filtering by Emp_id you do not need to group on it
var data = (from r in context.emp_leaves
where r.Emp_id == empid && r.status == "Approved"
group r by r.leave_type into g
select new
{
leave_type = g.Key,
leave_taken = g.Sum(x => x.leave_taken)
});