Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two tables Entity and EntityPoints

Entity table
VB
EntityId | Name
   1     | E1
   2     | E2
   3     | E3


EntityPoints Table
VB
EntityId | QId |Points

1        | 1   |100
1        | 2   |200
2        | 2   |350


my expected output is:
VB
Name | EntityId |QId  | Points
E1   |   1      | 2   | 200
E2   |   2      | 2   | 350
E3   |   3      |null | null




my problem is using this two tables how to get all Names in Entity table using where condition for EntityPoints Table QId ...
I try with following code but it not contain all Names of Entity table.How can I do it ?


C#
using (var db = new EntityContext())
{
    var PGEpoint = db.EntityPoints 
        .Join(db.Entity , p => p.EntityId , q => q.EntityId , (p, q) => new { p, q })
        .Where(w => w.p.QId == 2)
        .Select(m => new TestModel
        {
            pointGEID = m.q.EntityId ,
            PointGE_name = m.q.Name,
            Points = m.p.Points

        }).ToList();
    return PGEpoint;
}
Posted

1 solution

Something like this should work:
C#
using (var db = new EntityContext())
{
    return db.Entity.GroupJoin(
        db.EntityPoints.Where(p => p.QId == 2),
        entity => entity.EntityId,
        point => point.EntityId,
        (entity, points) => new 
        { 
            entity, 
            point = points.FirstOrDefault() 
        })
        .Select(m => new TestModel
        {
            pointGEID = m.entity.EntityId,
            PointGE_name = m.entity.Name,
            Points = (m.point == null) ? default(int?) : m.point.Points
        })
        .ToList();
}

Queryable.GroupJoin method (MSDN)[^]
How to: Perform Left Outer Joins (C# Programming Guide)[^]
 
Share this answer
 
Comments
sachika_88 8-May-15 1:14am    
Thank you so much for your solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900