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

  • SEARCH
  • Home
  • 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 8918881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:51:47+00:00 2026-06-15T05:51:47+00:00

I got the following LINQ Expression: Context.TableOne.Select( one => new { one.Column1, one.Column2, one.Column3,

  • 0

I got the following LINQ Expression:

Context.TableOne.Select(
            one =>
            new
            {
                one.Column1,
                one.Column2,
                one.Column3,
                one.Column4,
                one.Column5,
                one.Column6,
                one.Column7,
                one.Column8,
                one.Column9,
                TwoCount = one.TableTwo.Count()
            });

When I select it as this:

Context.TableOne.Select(
            one =>
            new
            {
                One = one,
                TwoCount = one.TableTwo.Count()
            });

I would have gotten a nested property “One” containing all the fields of “one”. This would require me (for example in a datagrid) to specify the FieldNames One.Column1 or One.Column2 instead of Column1 or Column2

Is it possible to include the TwoCount = one.TableTwo.Count()-Statement without needing to specify each and every column in this expression?

  • 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-15T05:51:48+00:00Added an answer on June 15, 2026 at 5:51 am

    When you use new {....} you are creating a new anonymous type. You are then setting the properties on that type with the object initializer syntax. It may be easier to understand if we break this down to what is actually happening.

    First you are creating a new type Foo. Let’s simplify this to 3 properties

    public class Foo
    {
        public object Column1 { get; set; }
        public object Column2 { get; set; }
        public int TwoCount { get; set; }
    }
    

    Now, let’s just look at the lambda expression that is your select. We can extract this to a function like below.

    public Foo CreateFoo(TableOne one)
    {
        Foo foo = new Foo();
        foo.Column1 = one.Column1;
        foo.Column2 = one.Column2;
        foo.TwoCount = one.TableTwo.Count();
    
        return foo;
    }
    

    As you can see, we must set each property. We could encapsulate this logic in a constructor or somewhere else in class Foo because it is not an anonymous type. However, with an anonymous type, there isn’t must you can do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got the following LINQ expression: var ageEntry = from entry in args where
I have the 2 following LINQ queries and I haven't quite got my head
I've got the following LINQ Statement: Dim PSNum As Integer = 66 Dim InvSeq
I've got the following c# code: using System; using System.Collections.Generic; using System.Linq; using System.Text;
Hi I got the following linq for ordering my input after lowest value. But
I have the following LINQ expression. I want calculate the sum of numeric values
I've got the following LINQ query to my IQueryable service Dim eventcount = EventService.QueryEvents().Count
I've got the following code: Dim query As IQueryable(Of someObject) = New ObjectQuery(Of someObject)(queryString,
I m building dynamic lambda expression.I got the following operators working for this code
I've got the following LINQ: var items = from n in db.Roles where n.importID

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.