Ef core search on child , order on parent - sql

I have two model Parent and Child and I want to retrieve all Distinct parent that has unitid = 5 and order by family
public class Parent
{
public int Id { get; set; }
public string Family { get; set; }
public List<Child> Children { get; set; }
}
public class Child
{
public int Id { get; set; }
public int ParentId { get; set; }
public int UnitId { get; set; }
public Parent parent { get; set; }
}
parent table has 500000 record and child table has 2000000 record . I use SelectMany And Distinct but it take 8s to retrieve records
var parents= Context.Set<Parent>()
.SelectMany(x => x.Children.DefaultIfEmpty(),
(u, r) => new {
Parent = u,
Child = r })
.Where(x=>x.Child.UnitId == 5)
.Select(m => new Parent{
Id = m.Parent.Id,
Family= m.Parent.Family
})
.Distinct()
.OrderBy(x=>x.Family)
.Take(30).Skip(0).ToList();
Tranlate to :
SELECT [t].[Id], [t].[Family] FROM (
SELECT distinct [a].[Id], [a].[Family]
FROM [Parent] AS [a]
LEFT JOIN [Children] AS [r] ON [a].[Id] = [r].[ParentId]
WHERE ([r].[unitid] = 5) AS [t] ORDER BY [t].[family] OFFSET 0 ROWS FETCH next 30 ROWS ONLY
Or
var parents = Context.Set<Children>()
.Include(x=>x.Parent)
.Where(x=>x.UnitId ==5)
.Select(m => new Parent{
Id = m.ParentId ,
Family = m.Parent.Family})
.Distinct()
.OrderBy(x=>x.Family)
.Take(30).Skip(0).ToList();
Translatr to :
SELECT [t0].[Id],[t0].[Family] FROM ( SELECT DISTINCT [t].[Id], [t].[Family]
FROM [Children] AS [r]
INNER JOIN (
SELECT [a].[Id] , [a].[Family]
FROM [Parent] AS [a]
) AS [t] ON [r].[ParentId] = [t].[Id]
WHERE ([r].[UnitId] = 5) AS [t0] ORDER BY [t0].[Family] OFFSET 0 ROWS FETCH next 30 ROWS ONLY
1 . When use order by id it is very fast
2 . when use order by family and take,skip it is slow
3 . family is indexed
4 . Ef Core 3.1
Which solution is Correct or better ?

Check these two queries for performance:
var parents =
from p in Context.Set<Parent>()
where p.Children.Any(c => c.UnitId == 5)
orderby p.Family
select p;
parents = parents
.Take(30)
.Skip(0)
.ToList();
And another one:
var parentIds =
from c in Context.Set<Child>()
where c.UnitId == 5
select c.ParentId
var parents =
from p in Context.Set<Parent>()
join id in parentIds.Distinct() on p.Id equals id
orderby p.Family
select p;
parents = parents
.Take(30)
.Skip(0)
.ToList();

Related

Linq to SQL use case and Lag equivalent

I have this SQL which I am trying to convert to LINQ , how can this be converted?
Is there an equivalent of Lag at all?
I see there is a case statement not sure how to use it
SELECT
ah.AuthHist_ID,
ah.F_ID,
CASE WHEN ah.AuthPIFlg = 1 OR ah.AuthPINVFlg = 1 THEN 'True' ELSE 'False' end AS chkReqPI,
lag(CASE WHEN ah.AuthPIFlg = 1 OR ah.AuthPINVFlg = 1 THEN 'True' ELSE 'False' end, 1, null) OVER (ORDER BY ah.f_id, ah.AuthHist_ID) AS prevChkReqPI,
ah.Cr8Dt,
lag(ah.Cr8Dt, 1, null) OVER (ORDER BY ah.f_id, ah.AuthHist_ID) AS prevCr8Dt,
cu.UserName AS Cr8UserName,
lag(cu.UserName, 1, null) OVER (ORDER BY ah.f_id, ah.AuthHist_ID) AS prevCr8UserName,
fh.UpdtDt,
FROM AuthHist Ah
LEFT JOIN User cu
ON Ah.Cr8User_ID = cu.User_ID
WHERE Ah.F_ID = #fid
return (from a in DbContext.AuthHist
join c DbContext.User on a.UpdtUserId equals c.UserId
where a.FId == fId
select new AuthHistEntity()
{
FId = a.FId,
checkReqPI = a.AuthPIflg = 1 || a.AuthPINVflg = 1 :
});
I have this SQL which I am trying to convert to LINQ , how can this be
converted?
You can use linq to do this, but it will be a bit more complicated.
For the case when in linq, you can directly use the ternary operator(?:) instead.
As for the Lag() function, we need to replace this function with another sql writing as follow:
(This is just to facilitate the understanding of the linq statement below, you do not need to change your sql statement)
select kh.AuthHist_ID,kh.F_ID,
CASE WHEN kh.AuthPIFlg = 1 OR kh.AuthPINVFlg = 1 THEN 'True' ELSE 'False' end AS chkReqPI,
CASE WHEN (ch.AuthPIFlg is null or ch.AuthPINVFlg is null) then Null when (ch.AuthPIFlg = 1 OR ch.AuthPINVFlg = 1) THEN 'True' ELSE 'False' end AS prevChkReqPI ,
kh.Cr8Dt,
ch.Cr8Dt AS prevCr8Dt ,
kh.UserName AS Cr8UserName,
cu.UserName AS prevCr8UserName
from
( select -1+row_number() over (order by ah.f_id,ah.AuthHist_ID) as row1, * from
(select * from AuthHist a left join User u on a.Cr8User_ID = u.User_ID) Ah) kh
left join (select * from AuthHist a left join User u on a.Cr8User_ID = u.User_ID) cu ON kh.row1 = cu.AuthHist_ID
left join AuthHist ch on kh.row1 = ch.AuthHist_ID where kh.F_ID =#fid
Because the types of some fields are uncertain, I have create the AuthHistEntity class as follow, you can modify some details according to your needs.
public class AuthHistEntity
{
public int AuthHist_ID { get; set; }
public int F_ID { get; set; }
public string chkReqPI { get; set; }
public string prevChkReqPI { get; set; }
public string Cr8Dt { get; set; }
public string prevCr8Dt { get; set; }
public string Cr8UserName { get; set; }
public string prevCr8UserName { get; set; }
}
Here is the linq writing, you can try it:
var newUser = (from a in DbContext.AuthHist
join u in DbContext.User on a.Cr8User_ID equals u.User_ID
select new { a, u }).ToList();
var newAuthList = (from t in newUser
select new
{
row_1 = t.a.AuthHist_ID - 1,
data = t
}).ToList();
int fid = 1;
var result = (from dt in
(from ch in newAuthList
join ah in DbContext.AuthHist on ch.row_1 equals ah.AuthHist_ID
into ot from otnew in ot.DefaultIfEmpty()
select new { T1 = ch, T2 = otnew == null ? new AuthHist() : otnew
}).ToList()
join nu in newUser on dt.T1.row_1 equals nu.a.AuthHist_ID
into yG from otnew in yG.DefaultIfEmpty()
where dt.T1.data.a.F_ID == fid
select new AuthHistEntity()
{
AuthHist_ID = dt.T1.data.a.AuthHist_ID,
F_ID = dt.T1.data.a.F_ID,
chkReqPI = (dt.T1.data.a.AuthPIFlg == 1 || dt.T1.data.a.AuthPINVFlg == 1) ? "True" : "False",
prevChkReqPI = (dt.T2.AuthPIFlg == null || dt.T2.AuthPINVFlg == null) ? null : ((dt.T2.AuthPIFlg == 1 || dt.T2.AuthPINVFlg == 1) ? "True" : "Flase"),
Cr8Dt = dt.T1.data.a.Cr8Dt,
prevCr8Dt = dt.T2.Cr8Dt == null ? null : dt.T2.Cr8Dt,
Cr8UserName = dt.T1.data.u.UserName,
prevCr8UserName = otnew == null ? null : otnew.u.UserName
}).ToList();

Filter values based on custom attribute in another table

I have two tables DefaultAttributes and CustomAttributes.
DefaultAttributeTable:
1. Id
2. Product
4. Description
CustomAtrributeTable:
1. Id
2. DefaultAttributeMappingId(FK from DefaultAttributeTable)
3. CustomAtrributeName
4. CustomeAttributeValue
Entries made by user:
user update the values Product -> Vegetables, Description -> House Hold Item, IsOrganic(Custom Attribute created) -> True and IsDisposable(Custom Attribute created) -> True
user update the values Product -> Fruits, Description -> House Hold Item, IsOrganic(Custom Attribute created) -> True and IsDisposable(Custom Attribute created) -> True
user update the values Product -> Plastic, Description -> House Hold Item, IsOrganic(Custom Attribute created) -> False and and IsDisposable(Custom Attribute created) -> False
Then the values will be updated in the table will
DeafaultAtrributeTable:
CustomAttributeTable:
I want to combine the two tables and select the Id, product, IsOrganic, IsDisposable and filter the values based on the isorganic column. Also the custom attribute column name should be taken form the CustomAtrributeTable. Please suggest to me how to achieve it in SQL and Linq Query. The filtered value should be
You may try this in SQL
select DA.Id, DA.Product, CA.CustomeAttributeValue as IsOrganic
from DeafaultAtrributeTable as DA inner join CustomAttributeTable as CA
on DA.Id = CA.DefaultAttributeMappingId
In LINQ
var query =
from DA in DeafaultAtrributeTable
join CA in CustomAttributeTable on DA.ID equals CA.DefaultAttributeMappingId
where CA.CustomeAttributeValue == true
select new { Id = DA.Id, Product = DA.Product, IsOrganic = CA.CustomeAttributeValue };
or LINQ extension method is
var query = DeafaultAtrributeTable // your starting point - table in the "from" statement
.Join(CustomAttributeTable , // the source table of the inner join
DA => DA.ID, // Select the primary key (the first part of the "on" clause in an sql "join" statement)
CA => CA.DefaultAttributeMappingId, // Select the foreign key (the second part of the "on" clause)
(DA, CA) => new { Id = DA.Id, Product = DA.Product, IsOrganic =
CA.CustomeAttributeValue }) // selection
.Where(RES => RES.CA.CustomeAttributeValue == true); // where statement
try this
public class Filtered
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public bool? IsOrganic { get; set; }
public bool? IsDisposable { get; set; }
}
var result = defaultAttributeTable.Join(customAtrributeTable, DAT => DAT.Id, CAT => CAT.DefaultAttributeTableId, (DAT, CAT) =>
new Filtered
{
//your join with null values for opposing isdisposable and isorganic
ProductId = DAT.Id,
IsDisposable = CAT.CustomAtrributeName == "IsDisposable" ? (bool?)CAT.CustomeAttributeValue : null,
IsOrganic = CAT.CustomAtrributeName == "IsOrganic" ? (bool?)CAT.CustomeAttributeValue : null,
ProductName = DAT.Product
}).GroupBy(q => q.ProductId) //group it by productid
.Select(q =>
new Filtered
{
//this will flatten opposing isorganic and isdisposable
ProductId = q.Key,
IsDisposable = q.First(e => e.IsDisposable.HasValue).IsDisposable.Value,
IsOrganic = q.First(e => e.IsOrganic.HasValue).IsOrganic.Value,
ProductName = q.First(e => e.ProductId == q.Key).ProductName
}).ToList();

Convert SQL sentence to LINQ

I need to create a LINQ sentence that does the same as this SQL sentence but I can't solve it...
SELECT l.Id, l.Url, t.Name, t.LanguageCode
FROM link l
LEFT OUTER JOIN linktr t ON t.Id = (SELECT tt.Id FROM linktr tt WHERE tt.LinkId = l.Id AND (tt.LanguageCode = 'en-EN' OR tt.LanguageCode = 'es-ES') ORDER BY (tt.LanguageCode = 'en-EN') DESC, tt.LanguageCode LIMIT 1)
ORDER BY l.OrderPos
Table Link
Id, Url
Table LinkTr
Id, LinkId, Name, LanguageCode
The table link contains the Url of the link while the LinkTr contains the name and it can cointain multiple languages
I want to select all the links we have in the links database and for each link I want to show JUST ONE language, the one that corresponds to the current language if exists, but if it doesn't exist I have to show the one that corresponds to the default language. If no one of them exist it wouldn't show that record.
For this example the default language is es-ES and the current one is en-EN.
How can I do this using LINQ?
Thanks in advance
EDIT
If I use this LINQ expression it works only if I comment the ORDER line, but without the ORDER line (.OrderBy(p3 => p3.LanguageCode == currentLanguage)) it takes the first record in the db by Id, but not the current language first.
var queryString = from i in _dbContext.Links
let p = _dbContext.LinksTrs.Where(p2 => i.Id == p2.LinkId && (p2.LanguageCode == currentLanguage || p2.LanguageCode == defaultLanguage))
.OrderBy(p3 => p3.LanguageCode == currentLanguage)
.FirstOrDefault()
orderby i.OrderPos
select new LinksWebListModel
{
Id = i.Id,
Name = (p == null) ? "" : p.Name,
Url = i.Url,
Summary = (p == null) ? "" : p.Summary,
};
EDIT 2
I'm suing EF6 and my entities, which I can't change, are these:
public class Link : EntityBase
{
public int Id { get; set; }
public int OrderPos { get; set; }
public string Url { get; set; }
public virtual IEnumerable<LinkTr> Translations { get; set; }
}
public class LinkTr
{
public int Id { get; set; }
public string Name { get; set; }
public string LanguageCode { get; set; }
public int LinkId { get; set; }
public virtual Link Link { get; set; }
}
EDIT 3
it would be something like this...
var queryString = (from entity in _dbContext.Links
let translation = _dbContext.LinksTrs
.Where(p2 => p2.LinkId == entity.Id)
.Where(p3 => p3.LanguageCode == currentLanguage || p3.LanguageCode == defaultLanguage)
.OrderBy(p4 => p4.LanguageCode)
.FirstOrDefault()
orderby entity.OrderPos descending
select new LinksWebListModel()
{
Id = entity.Id,
Name = translation.Name,
Url = entity.Url,
Summary = translation.Summary,
});
buit the problems here are 2:
- I need to return the record (Links) only if there is a row in the sub query (LinksTrs)
- Also I'd need to order the subquery by "currentLanguage" first and then "defaultLanguage" and then the rest. If I add this to the sub query I get an error:
.OrderBy(p4 => p4.LanguageCode == currentLanguage)
.ThenBy(p5 => p5.LanguageCode == defaultLanguage)
.ThenBy(p6 => p6.LanguageCode)
Do you have an idea on how to order by certain values, for example if I have these languages "es-ES", "en-EN", "it-IT", "fr-FR" for example to get first "it-IT", then "es-ES" and then order by name...

Nhibernate Join 2 QueryOver

I can't find how to join two different QueryOver, group by and perform a substraction in the select.
Say you have :
public class EntityA
{
public virtual int Id;
public virtual string Reference;
public virtual int Quantity;
[Some properties]
}
public class EntityB
{
public virtual int Id;
public virtual int EntityAId;
[Some properties]
}
If i translate my query in pseudo-SQL, i would like to have :
SELECT A.Id, A.Reference, A.Quantity - COALESCE(DERIV_B.TOTAL, 0)
FROM EntityA A
LEFT JOIN (
SELECT B.EntityAId, COUNT(B.Id) AS TOTAL
FROM EntityB B
GROUP BY B.EntityAId) DERIV_B
ON A.Id = DERIV_B.EntityAId
WHERE (A.Quantity - COALESCE(DERIV_B.TOTAL, 0)) >= 0
I can have the subquery on EntityB via QueryOver, but i can't join on EntityA :
var entitiesB = GetCurrentSession().QueryOver<EntityB>().SelectList(select => select.SelectGroup(x => x.EntityAId).SelectCount(x => x.Id));
var entitiesA = GetCurrentSession().QueryOver<EntityA>(). ???
I tried to store the entitiesB in an alias and the perform a JoinAlias on it but i have an exception because it can't retrieve my alias.
Do you have any solution ?
I don't want to create a reference between these two entites.
Short answer is not , you can't do QueryOver if your entities are not connected through model.
One solution would be to use NHibernate.Linq and subqueries
var session = GetCurrentSession();
var entityBQuery = session.Query<EntityB>();
var entityAQuery = session.Query<EntityA>()
.Select(eA=>new { Id = eA.Id,
Description = eA.Description,
Quantity = eA.Quantity - entityBQuery.Where(eb=>eb.EntityAId = eA.Id).Count()
});

how to compare string linq with sub query with another linq with sub query

I Have 4 tables:
Position Table:
| Position | PositionId |
| driver | 1 |
| clerk | 2 |
position Skill table:
| SkillId | skill | PositionId |
| 1 | driving | 1 |
| 2 | drifting | 1 |
Worker table:
| Name | WorkerId |
| John | 1 |
| alex | 2 |
Worker skill table:
| skillId | skill | WorkerId |
| 1 | driving | 1 |
| 2 | drifting | 1 |
I join the position table with position Skill table
and worker table with worker skill
What I'm having trouble with is how can I compare the two joined tables to have a result of
for example:
I need to know who's worker have all the specific skills that the position have
Like:
I Select position with positionId of 1 and have the skillname of driving and drifting
I need to get the Worker with the same skills with driving and drifting also
so far i got this:
var PositionsWithSkills = (from a in db.Client_Customer_Position
where a.ID == position
select new
{
PositionID = a.ID,
RequiredSkills = (from b in db.Client_Customer_Position_Skills
where b.ClientCusPosId == a.ID
select b.SkillName)
}).ToList();
var WorkersWithSkills = (from x in db.Workers
select new
{
workerId = x.ID,
Skills = (from y in db.Worker_Skills
where y.Worker_ID == x.ID
select y.SkillName)
}).ToList();
var PositionWithSkilledWorkers = (from pos in PositionsWithSkills
select new
{
PositionId = pos.PositionID,
Workers = (from worker in WorkersWithSkills
where pos.RequiredSkills.All(skill => worker.Skills.Any(workerSkill => workerSkill == skill))
select worker.workerId)
}).ToList();
the two query works well.. but the last query where i must compare the two query =.. i cant get the worker id
and can i turn this to a stored proc?
Sorry if am wrong. What I got to know from your question is you want the workers list satisfying all the skills of the position you pass. If this is what you want you may try this:
var workerWithallSkill = (from u in db.workerList join x in db.workerSkillList on u.WorkerId equals x.WorkerId
where ((from y in db.workerSkillList where y.WorkerId == u.WorkerId select y).Count() == (from p in db.positionSkillList where p.PositionId == 1("pass your positionId here") select p).Count())
select u).ToList().Distinct();
or if you want to use lambda expression you can use this
var workerWithallSkill = (from u in workerList join x in workerSkillList on u.WorkerId equals x.WorkerId where (workerSkillList.Where(y=> y.WorkerId == u.WorkerId).Count() == positionSkillList.Where(p=>p.PositionId == 1).Count()) select u).ToList().Distinct();
For more understanding you can try the below code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication8
{
class Program
{
static void Main(string[] args)
{
IList<Position> positionList = new List<Position>() {
new Position(){ position="Driver", PositionId=1}
,new Position(){ position="clerk", PositionId=2}
};
IList<PositionSkill> positionSkillList = new List<PositionSkill>() {
new PositionSkill(){ Skill = "driving",skillid = 1,PositionId = 1}
,new PositionSkill(){ Skill = "drifting",skillid = 2,PositionId = 1}
};
IList<Worker> workerList = new List<Worker>() {
new Worker(){ name = "John",WorkerId = 1}
,new Worker(){ name = "alex",WorkerId = 2}
};
IList<WorkerSkill> workerSkillList = new List<WorkerSkill>(){
new WorkerSkill(){Skill = "driving",skillid = 1,WorkerId = 2}
, new WorkerSkill(){Skill = "drifting",skillid = 2,WorkerId = 2}
};
var workerWithallSkill = (from u in workerList join x in workerSkillList on u.WorkerId equals x.WorkerId where (workerSkillList.Where(y => y.WorkerId == u.WorkerId).Count() == positionSkillList.Where(p => p.PositionId == 1).Count()) select u).ToList().Distinct();
foreach (var worker in workerWithallSkill)
{
Console.WriteLine(worker.name);
}
Console.ReadLine();
}
}
public class Position
{
public string position { get; set; }
public int PositionId { get; set; }
}
public class PositionSkill
{
public int skillid { get; set; }
public string Skill { get; set; }
public int PositionId { get; set; }
}
public class Worker
{
public string name { get; set; }
public int WorkerId { get; set; }
}
public class WorkerSkill
{
public int skillid { get; set; }
public string Skill { get; set; }
public int WorkerId { get; set; }
}
}
if a worker has skills from different positions the above code will not work, if this is the scenario try the below code:
var WorkerPositionSkill = from p in db.positionSkillList join q in db.workerSkillList on p.skillid equals q.skillid select new { posSkill = p, workerSkill = q };
var workerWithallSkill = (from u in db.workerList join x in db.workerSkillList on u.WorkerId equals x.WorkerId where (WorkerPositionSkill.Where(y => y.workerSkill.WorkerId == u.WorkerId && y.posSkill.PositionId == 1).Count() == db.positionSkillList.Where(p => p.PositionId == 1).Count()) select u).ToList().Distinct();
This is highly unlikely to work with Linq To SQL because...its a huge steaming pile of #$%&. But this Linq query should given a sufficiently magical IQueryProvider give the right SQL. I've seen some very magical things come from Entity Framework.
var PositionsWithSkills = from a in db.Client_Customer_Position
where a.ID == position
select new
{
PositionID = a.ID,
RequiredSkills = (from b in db.Client_Customer_Position_Skills
where b.ClientCusPosId == a.ID
select b.SkillName)
};
var WorkersWithSkills = from x in db.Workers
select new
{
workerId = x.ID,
Skills = (from y in db.Worker_Skills
where y.Worker_ID == x.ID
select y.SkillName)
};
var PositionWithSkilledWorkers = from pos in PositionsWithSkills
from worker in WorkersWithSkills
where pos.RequiredSkill.All(worker.Skills.Contains)
group worker.Name by pos.PositionID;
PS please learn to use associations as opposed to join/where. If you are going to use join/where, you might as well just use SQL.
var PositionsWithSkills = (from a in Positions select new {
PositionID = a.PositionId,
RequiredSkills = (from b in PositionSkills where b.PositionId == a.PositionId select b.skillId).ToList()
}).ToList();
var WorkersWithSkills = (from x in Workers select new {
Name = x.Name,
Skills = (from y in WorkerSkills where y.WorkerId == x.WorkerID select y.skillId).ToList()
}).ToList();
var PositionWithSkilledWorkers = (from pos in PositionsWithSkills select new {
PositionId = pos.PositionID,
Workers = (from worker in WorkersWithSkills where pos.RequiredSkills.All(skill => worker.Skills.Any(workerSkill => workerSkill == skill)) select worker.Name).ToList()
}).ToList();
i think, your database's tables have not been designed correctly...
you need a relation between worker skill and position skill, i think your tables must be desinged like this:
Skill table: SkillID, Skill
Position table: PositionID, Position
PositionSkill table: ID, SkillID, PositionID
Worker table: WorkerID, Name
WorkerSkill table: ID, SkillID, WorkerID
but by this way you designed your tables, if assume skill field (description of skills) are the same in worker skill and position skill, we can use this as a relation, and your query can be like this:
// skills of specific position
var PositionSkills = Context.PositionSkill.Where(u => u.PositionId == 1);
var WorkersWithSkills = Context.Worker
.Join(Context.WorkerSkill,
worker => worker.WorkerId,
workerSkill => workerSkill.WorkerId,
(worker, workerSkill) => new { worker, workerSkill })
.GroupBy(u => u.worker)
.Select(u => new
{
u.Key.WorkerId,
u.Key.Name,
Skills = u.Select(t => t.workerSkill.skill)
});
var SkilledWorkers = WorkersWithSkills
.Where(u => PositionSkills.All(t => u.Skills.Contains(t.skill)))
.ToList();
if you wont change your database's tables, you can add a join table between position skill and worker skill like:
WorkerPositionSkill: PositionSkillID, WorkerSkillID
Here is a LinqPad program that returns the result, { Worker = John, Position = Driver}. If I understand your requirements you want to find a worker who satisfies the conditions where the worker has all the skills required for Position = 1, which is driving and drifting skills. The query returns two rows the following [{worker = John, Position = Driver}, {worker = John, Position = Driver}]. I had to use distinct to display it once. The reason for two rows is he satisfies both driving and drifting job skills. If the position required 4 skills in which the worker met, there would be 4 duplicate rows. The unique fixes that problem. Hope this helps you along.
I created this solution in LinqPad, which is great tool with hundred's of very well documented linq query examples.
void Main()
{
// Table Setup
// ************
var position = new List<Position>();
position.Add(new Position { Id = 1, Name = "driver" });
position.Add(new Position { Id = 2, Name = "clerk" });
var positionSkill = new List<PositionSkill>();
positionSkill.Add(new PositionSkill { Id = 1, Skill = "driving", PositionId = 1 });
positionSkill.Add(new PositionSkill { Id = 2, Skill = "drifting", PositionId = 1 });
var worker = new List<Worker>();
worker.Add(new Worker { Id = 1, Name = "John" });
worker.Add(new Worker { Id = 2, Name = "alex" });
var workerSkill = new List<WorkerSkill>();
workerSkill.Add(new WorkerSkill { Id = 1, Skill = "driving", WorkerId = 1 });
workerSkill.Add(new WorkerSkill { Id = 2, Skill = "drifting", WorkerId = 1 });
// The Query
// *********
var positionValue = 1;
var r = from p in position
join ps in positionSkill on p.Id equals ps.PositionId
join ws in workerSkill on ps.Skill equals ws.Skill
join w in worker on ws.WorkerId equals w.Id
where p.Id == positionValue
select new {
PositionName = p.Name,
WorkerName = w.Name
};
// Get Distinct Names
r.Distinct().Dump();
}
// Define other methods and classes here
public class Position
{
public int Id { get; set; }
public string Name { get; set; }
}
public class PositionSkill
{
public int Id { get; set; }
public string Skill { get; set; }
public int PositionId { get; set; }
}
public class Worker
{
public int Id { get; set; }
public string Name { get; set; }
}
public class WorkerSkill
{
public int Id { get; set; }
public string Skill { get; set; }
public int WorkerId { get; set; }
}