Isaiah Nelson Isaiah Nelson - 4 months ago 29
C# Question

Select all columns on an object with Linq

I have a query:

var transactions = from t in db.Transactions
where t.SellingPrice != 0
select new { t.CommissionPercent, t.SellingPrice };


But in reality, I need to avoid using an anonymous type because it is readonly and select all the properties/columns on my "transaction" item.

I would have thought that it would be something like this:

var transactions = from t in db.Transactions
where t.SellingPrice != 0
select t.SellingPrice, t.CommissionPercent, t.Etc...


Or...

var transactions = from t in db.Transactions
where t.SellingPrice != 0
select t.SellingPrice
select t.CommissionPercent
select t.Etc...


Is there no way to retrieve everything the object has for properties and pass it to the Ienumerable?

Answer

If you want to avoid anonymous types and get everything, why not just return an IEnumerable of the original transaction item?

var transactions = from t in db.Transactions
                        where t.SellingPrice != 0 
                        select t;
Comments