ALIasing fields in linq

It seems like it should be pretty simple, but I couldn't figure it out or find anything on it. My request looks something like this:

from o in objects
          select new
          {
            o.ID,
            o.member.Number,
            o.member.Date,
            o.member.total,
            o.SequenceNumber,
            o.InputDate,
            o.Amount,
            o.Discount,
            Balance = o.Balance(),
            o.otherMember.CreatedBy,
          }

      

Please note that there are several times when I refer to oemember. In real life it looks like 20 times (I am collecting dataset for a grid). I would like to do it like this:

select new
          {
            o.ID,
            m.Number,
            m.Date,
            m.total,
            o.SequenceNumber,
            o.InputDate,
            o.Amount,
            o.Discount,
            Balance = o.Balance(),
            o.otherMember.CreatedBy,
          }

      

But I'm not sure about the correct syntax. Is it possible for it to be at the top or connected to something?

+2


source to share


1 answer


You can do:



from o in objects
let m = o.member
select new { /* as per question */ };

      

+9


source







All Articles