Accessing the members of an anounimous type

  • Thread starter Thread starter Juan Dent
  • Start date Start date
J

Juan Dent

Hi,

I have a query like:

var p = (from c in context.Customers
select new { Customer = c, MaxId = context.Customers.Max(
s=>s.Id) } );

I know my resultset is going to be an IEnumerable of Customers and Int32,
but how do I access them?
I trid p.Customer but the compiler complains.

So?
 
Hello Juan Dent,

Thanks for using Microsoft Newsgroup Support Service, my name is Ji Zhou
[MSFT] and I will be working on this issue with you.

My understanding of your issue is how to access the Customer column in the
implicit type p which is returned from the LINQ statement. Please correct
me if I have misunderstood the issue.

The p is a implicit type collection. Thus, we need to access one item's
Customer object using a foreach loop. I achieve this by using the following
codes. Note the p is an implicit type, so we also need to declare the item
as var in the foreach loop.

var p = (from c in context.Customers
select new
{
Customer = c,
MaxId = context.Customers.Max(
s => s.Id)
}
);

foreach (var item in p)
{
Console.WriteLine("Name={0}, MaxID={1}", item.Customer.Name, item.MaxID);
}

You can get detailed information about the implicit type and var keyword in
the following MSDN document:
http://msdn.microsoft.com/en-us/library/bb383973.aspx

Please feel free to let me know, if you have any future questions on this.
I will do my best to provider future help.


Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hello Juan Dent,

Thanks for using Microsoft Newsgroup Support Service, my name is Ji Zhou
[MSFT] and I will be working on this issue with you.

My understanding of your issue is how to access the Customer column in the
implicit type p which is returned from the LINQ statement. Please correct me
if I have misunderstood the issue.

The p is a implicit type collection. Thus, we need to access one item’s
Customer object using a foreach loop. I achieve this by using the following
codes. Note the p is an implicit type, so we also need to declare the item
as var in the foreach loop.

var p = (from c in context.Customers
select new
{
Customer = c,
MaxId = context.Customers.Max(
s => s.Id)
}
);

foreach (var item in p)
{
Console.WriteLine("Name={0}, MaxID={1}", item.Customer.Name, item.MaxID);
}

You can get detailed information about the implicit type and var keyword in
the following MSDN document:
http://msdn.microsoft.com/en-us/library/bb383973.aspx

Please feel free to let me know, if you have any future questions on this. I
will do my best to provider future help.


Best regards,
Ji Zhou ([email protected], remove ‘online.’)
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Juan,

It can be that you want the collection (as Yi showed you) or that you only
want the first.

Than your next statement can be

var customer = p.First();

Cor
 
Back
Top