Rename field in a lambda

Asked

Viewed 89 times

1

In SQL I do so:

select campo1 as teste from tabela

And in a lambda? How would I do? I have this lambda with several tables and the field ValuePayment is repeated several times.

var qry = db.User.Join(db.Solicitation, user => user.UserID, sol => sol.UserID, (user, sol) => new { user, sol })
                    .Join(db.Payments, sol => sol.sol.SolicitationID, py => py.SolicitationID, (sol, py) => new { sol, py })
                    .Join(db.RefundKM, sol => sol.sol.sol.SolicitationID, km => km.SolicitationID, (sol, km) => new { sol, km })
                    .Join(db.Refund, sol => sol.sol.sol.sol.SolicitationID, re => re.SolicitationID, (sol, re) => new { sol, re })
                    .Select(syn => new
                    {
                        syn.sol.sol.sol.user.EmployeeID,
                        syn.sol.sol.sol.user.EmployeeStatus,
                        syn.sol.sol.sol.user.EmployeeFirstName,
                        syn.sol.sol.sol.user.Grade,
                        syn.sol.sol.sol.sol.SolicitationID,
                        syn.sol.sol.sol.sol.StatusSolicitation,
                        syn.sol.sol.sol.sol.DateFinancing,
                        syn.sol.sol.sol.sol.Manufacturer,
                        syn.sol.sol.sol.sol.Chassi,
                        syn.sol.sol.sol.sol.Model,
                        syn.sol.sol.sol.sol.ValueProperty,
                        syn.sol.sol.sol.sol.ValueGranted,
                        syn.sol.sol.py.PaymentStatus,
                        syn.sol.sol.py.ValuePayment,
                        syn.sol.sol.py.ValueInterest,
                        syn.sol.km.ValuePayment,
                        syn.re.ValuePayment

                    });

2 answers

5

syn.sol.sol.sol.sol. surely it’s one of the biggest absurdities I’ve ever seen in code and something tells me it shouldn’t stop there, but I think this is what you want:

var qry = db.User.Join(db.Solicitation, user => user.UserID, sol => sol.UserID, (user, sol) => new { user, sol })
                .Join(db.Payments, sol => sol.sol.SolicitationID, py => py.SolicitationID, (sol, py) => new { sol, py })
                .Join(db.RefundKM, sol => sol.sol.sol.SolicitationID, km => km.SolicitationID, (sol, km) => new { sol, km })
                .Join(db.Refund, sol => sol.sol.sol.sol.SolicitationID, re => re.SolicitationID, (sol, re) => new { sol, re })
                .Select(syn => new {
                    EmployeeID = syn.sol.sol.sol.user.EmployeeID,
                    EmployeeStatus = syn.sol.sol.sol.user.EmployeeStatus,
                    EmployeeFirstName = syn.sol.sol.sol.user.EmployeeFirstName,
                    Grade = syn.sol.sol.sol.user.Grade,
                    SolicitationID = syn.sol.sol.sol.sol.SolicitationID,
                    StatusSolicitation = syn.sol.sol.sol.sol.StatusSolicitation,
                    DateFinancing = syn.sol.sol.sol.sol.DateFinancing,
                    Manufacturer = syn.sol.sol.sol.sol.Manufacturer,
                    Chassi = syn.sol.sol.sol.sol.Chassi,
                    Model = syn.sol.sol.sol.sol.Model,
                    ValueProperty = syn.sol.sol.sol.sol.ValueProperty,
                    ValueGranted = syn.sol.sol.sol.sol.ValueGranted,
                    PaymentStatus = syn.sol.sol.py.PaymentStatus,
                    ValuePayment = syn.sol.sol.py.ValuePayment,
                    ValueInterest = syn.sol.sol.py.ValueInterest,
                    ValuePayment = syn.sol.km.ValuePayment,
                    ValuePaymentRe = syn.re.ValuePayment
                });

I put in the Github for future reference.

4


Only match the property with a name of your preference, for example:

empId = syn.sol.sol.sol.user.EmployeeID

in your code something like this:

var qry = db.User.Join(db.Solicitation, user => user.UserID, sol => sol.UserID, (user, sol) => new { user, sol })
    .Join(db.Payments, sol => sol.sol.SolicitationID, py => py.SolicitationID, (sol, py) => new { sol, py })
    .Join(db.RefundKM, sol => sol.sol.sol.SolicitationID, km => km.SolicitationID, (sol, km) => new { sol, km })
    .Join(db.Refund, sol => sol.sol.sol.sol.SolicitationID, re => re.SolicitationID, (sol, re) => new { sol, re })
    .Select(syn => new
    {
        syn.sol.sol.sol.user.EmployeeID,
        syn.sol.sol.sol.user.EmployeeStatus,
        syn.sol.sol.sol.user.EmployeeFirstName,
        syn.sol.sol.sol.user.Grade,
        syn.sol.sol.sol.sol.SolicitationID,
        syn.sol.sol.sol.sol.StatusSolicitation,
        syn.sol.sol.sol.sol.DateFinancing,
        syn.sol.sol.sol.sol.Manufacturer,
        syn.sol.sol.sol.sol.Chassi,
        syn.sol.sol.sol.sol.Model,
        syn.sol.sol.sol.sol.ValueProperty,
        syn.sol.sol.sol.sol.ValueGranted,
        syn.sol.sol.py.PaymentStatus,
        p1 = syn.sol.sol.py.ValuePayment,
        syn.sol.sol.py.ValueInterest,
        p2 = syn.sol.km.ValuePayment,
        p3 = syn.re.ValuePayment
    });

in this example how has 3 equal I renamed to p1, p2 and p3, but, the name may be of your preference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.