Must I use an anonymous type when getting more than one column in a LINQ query result?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
In my last post here, I asked about this query:
<pre class="prettyprint var results = from charge in m_context.Charges
orderby charge.Charge1
select charge.Charge1;<br/>[/code]
From the little I understand now, I think that if I want to get more than one column from the Charges table, I have to create an anonymous type for the result, like this:
<pre class="prettyprint var results = from charge in m_context.Charges
orderby charge.Charge1
select new { charge.Charge1, charge.Base };[/code]
But do I have to use the anonymous type? I tried just using "select charge.Charge1, charge.Base" in the last line, but its a syntax error.
Ill take a stab at answering my own question:
I think I am stuck with the anonymous type because the result of a LINQ query has to be a collection (note: term used loosely) of objects of the same type. If Im trying to return multiple values, they have to be combined into a single type through
the anonymous type mechanism.
That leads to this question: Can I create my own class to hold the result of a multi-valued LINQ query? Something like this?
<pre class="prettyprint public class ResultClass
{
long m_charge;
string m_base;
};

IEnumerable<ResultClass> results =
from charge in m_context.Charges
orderby charge.Charge1
select ResultClass.m_charge = charge.Charge1,
ResultClass.m_base = charge.Base; [/code]
<br/>
Obviously thats not going to work. Im just hoping it will let you understand my question.
Thanks again!
RobR


View the full article
 
Back
Top