Recursive get all childrens using LINQ C# - c#

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

Related

C# forming Linq Query using .FindByExp

Table 1
Id Name
1 Tank 1
2 Tank 2
3 Tank 3
4 Tank 4
Table 2
Id Name
1 Tank 1
2 Tank 2
Result List it should return
Id Name
1 Tank 1
2 Tank 2
3 Tank 3
4 Tank 4
I need to form a linq query using .FindByExp only. Like below i need to use joins and arrive at above list Result.
var tankLst = context.Tank.FindByExp(a => a.Id== S.Id).ToList();
var distinctNames = context.Table1.FindAll().Select(x=>x.Name).Distinct().ToArray();
var tab1 = context.Table1.FindAll().ToList();
var tab2 = context.Table2.FindAll().Where(x=> !distinctNames.Contains(x.Name)).ToList();
var resultList = tab1.Concat(tab2).OrderBy(x=>x.Name);

How to group certain element then after add item of specific criteria doing linq to sql in c#

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 !

LINQ Query multiple orderby of joined tables

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();

Linq query to get count with multiple column values coming from condition on another column

I have a table A as below:
Table A
ID ScheduleID UserCode
1 4 D
2 4 A
3 1 B
4 2 D
5 4 C
6 6 A
7 2 B
I want to get count of rows for all those scheduleID values that corresponds to a specific UserCode. e.g., for UserCode = D, corresponding ScheduleIDs are 4 & 2. Count of all rows with ScheduleID = 2 & 4 is 5.
I have tried following query:
int count = A.Count(j => j.ScheduleID.Equals
(A.Any(k => k.UserCode == D)));
This gives run-time error
"Unable to cast the type 'System.Boolean' to type 'System.Object'
. Can anyone suggest what should be the accurate query for this scenario?
var query = A.Where(p=> p.UserCode == D).Select(x=> x.ScheduleID);
var count = A.Count(p=> query.Contains(p.ScheduleID));
you can replace query in the second line probably with the top line, to make it one line, but i Personaly think this is cleaner
This is my solution to get the same result :
int count = (from q1 in A
join q2 in A on q1.ScheduleID equals q2.ScheduleID
where q2.UserCode == "D"
select q1).Count();

convert an sql query into linq to entities method based

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..

Categories

Resources