1

I am new to lambda. how can i write below code using lambda. thanks

User user1 = (from u in db.Users
                                  join h in db.HCM_SMS_ROLE_MAPPINGs
                                  on u.roleID equals h.SMS_Role_ID
                                  where (u.Employee_Code == employeeCode && u.isDeleted == false && h.Is_Active_App == true)
                                  select u).Distinct().FirstOrDefault();
3
  • 1
    lq2m.azurewebsites.net Commented Apr 4, 2019 at 7:47
  • 1
    I think you meant from query syntax to method syntax because both would be considered LINQ Commented Apr 4, 2019 at 8:02
  • can you share your model classes? Commented Apr 4, 2019 at 8:03

2 Answers 2

2

This should work for you:

db.Users.Join(db.HCM_SMS_ROLE_MAPPINGs, u => u.roleID, h => h.SMS_Role_ID, (u, h) => new
{
u, h
}
).Where(x=> x.u.Employee_Code == employeeCode && x.u.isDeleted == false && x.h.Is_Active_App == true).Select(x=> x.u).Distinct().FirstOrDefault()

Clean and Simple! Hope it helps

Sign up to request clarification or add additional context in comments.

6 Comments

that's the correct lambda version for the query syntax in the question, though please remember for Distinct to work on the complete object either pass IEqualityComparer or implement IEquatable
@MrinalKamboj can you please suggest an edit explaining that? I don't understand how to do it correctly.
that's the overload of the Disntinct, which can be supplied an IEqualityComparer, or the class needs to implement IEquatable, so when class doesn't implement IEquatable, then we go for the external IEqualityComparer, these are points which helps in resolving issues in a class object doing the equality comparison else it goes for the reference comparison instead of the value / property comparison which we are generally expecting for the primitive types
@MrinalKamboj is it like I make a class inherit from IEqualityComparer of type ClassToBeCompared and add a method Equals to it? like this: class Compare : IEqualityComparer<ClassToBeCompared> { public bool Equals(ClassToBeCompared x, ClassToBeCompared y) { return x.Numf == y.Numf; } }
yes you can use IEqualityComparer derived class if the class doesn't implement IEquatable by default, idea is to do value comparison for the reference types, which might otherwise check the reference equality by default
|
0

My method is to right click the lightbulb in the left hand side bar and select 'Convert LINQ to method chain', which gives this:

User user1 = (db.Users
                .Join(db.HCM_SMS_ROLE_MAPPINGs, 
                   u => u.roleID, 
                   h => h.SMS_Role_ID, 
                   (u, h) => new { u, h })
                .Where(@t => (u.Employee_Code == employeeCode && u.isDeleted == false && h.Is_Active_App == true))
                .Select(@t => u))
                .Distinct()
                .FirstOrDefault();

I think this is a Resharper feature.

1 Comment

great though you may add an explanation, not everyone would have the Resharper

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.