Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8215335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:45:05+00:00 2026-06-07T11:45:05+00:00

Check the following program below from the book Essential LINQ: class Customer { public

  • 0

Check the following program below from the book “Essential LINQ”:

class Customer
    {
     public string CustomerID {get;set;}
     public string ContactName {get;set;}
     public string City {get;set;}
     public static List<Customer> GetCustomers()
     {
        return new List<Customer>
        {
            new Customer {CustomerID = "ALFKI", ContactName = "Maria Anders", City = "Berlin"},
            new Customer {CustomerID = "ANATR", ContactName = "Sns Trujillo", City = "Mexico D.F."},
            new Customer {CustomerID = "ANTON", ContactName = "Antonio Moreno", City = "Mexico D.F."}
        };
     }
    }
void Main()
{
    var query = from c in Customer.GetCustomers()
                where c.City == "Mexico D.F."
                select new { c.City,  c.ContactName};
    foreach (var cityandcontact in query)
    {
        Console.WriteLine(cityandcontact);
    }
}

In the LINQ Query, why it gives me error if I remove “new” from line:
select new {c.City, c.ContactName};

why we can’t just write like this

select c.City, c.ContactName;

what is the logic here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T11:45:07+00:00Added an answer on June 7, 2026 at 11:45 am

    Queries return enumerations of objects of the same class. It can be any class, but it needs to be just one for all rows. You cannot return pairs of objects (a city and a contact name) without combining them into a single object. In this case, you are combining them into an object of anonymous type, with two properties.

    On a side note, there is no advantage to using Select method at all, because both properties come from the same Customer class. In fact, there is less efficient than returning customer objects unchanged, like this:

    var query = from c in Customer.GetCustomers()
                where c.City == "Mexico D.F.";
    foreach (var c in query)
    {
        Console.WriteLine("{0} {1}", c.City, c.ContactName);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code snippet is from book Effective C#, public event AddMessageEventHandler Log; public
I have a question on constant objects. In the following program: class const_check{ int
Check the following code: ​<div onclick=alert('Hi, from outer div!');> <button onclick=alert('Hi, from button!');>Tha button</button>,
Please check out my following code... public enum LogType { Debug, Info, Warn, Error,
Based on the following question: Check if one string is a rotation of other
Please check the below program. #include <stdio.h> struct st { int a ; }
Hi I have the following program to check the send buffer size for a
I am trying to modify the code below to check the input from a
I wrote the following program to check strings for balanced parenthesis: isBalanced xs =
Please check the following example: http://www.esaer.com.br/csstest/ If the vertical scrollbar doesn't appear, please resize

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.