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 120913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:53:12+00:00 2026-05-11T03:53:12+00:00

If I have something like: var query = from children in _data.Children where children.ChildId

  • 0

If I have something like:

var query = from children in _data.Children     where children.ChildId == childId     select new CustomModel.MyChild     {         ChildId = children.ChildId,         Name = children.ChildName     };  return query.FirstOrDefault(); 

Where I want the resultant object to be my custom model.

Can I handle the custom model instantiation in a different method, which could be reused if I had multiple linq queries that all generated a custom child model?

For example,

var query = from children in _data.Children     where children.ChildId == childId     select CreateMyCustomChild([param ??]);  return query.FirstOrDefault(); 

This may well be impossible, I don’t know, but what would the method signature be like if it is possible?

I’m only thinking reuse for when multiple linq queries contain duplicate object initialisation code.

Thanks

  • 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. 2026-05-11T03:53:13+00:00Added an answer on May 11, 2026 at 3:53 am

    It really depends on what version of LINQ you’re using. If you’re using LINQ to SQL, I don’t believe you can call arbitrary methods in the query. The query translator wouldn’t know what to do with the method call

    If you’re using LINQ to Objects, you’re absolutely fine to do it, like this:

    var query = from children in _data.Children     where children.ChildId == childId     select CreateMyCustomChild(children)  return query.FirstOrDefault();  // Elsewhere  public CustomModel.MyChild CreateMyCustomChild(OtherChild child) {     return new CustomModel.MyChild     {         ChildId = child.ChildId,         Name = child.ChildName     }; } 

    (Side note: I’d call the range variable in the query ‘child’ rather than ‘children’ as at any one time it only represents a single child.)

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

Sidebar

Ask A Question

Stats

  • Questions 73k
  • Answers 73k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I believe this will destroy the privileges you've set. You… May 11, 2026 at 1:58 pm
  • added an answer I'd always use the libraries: No need to fork twice… May 11, 2026 at 1:58 pm
  • added an answer So, there are LOTS of .WAV formats, here's some documentation:… May 11, 2026 at 1:58 pm

Related Questions

If I have something like a UILabel linked to a xib file, do I
If I have something like a loop or a set of if/else statements, and
Doesn't an ORM usually involve doing something like a select *? If I have
Let's say I have some code like this if(isset($_GET['foo'])) //do something if(isset($_GET['bar'])) //do something
I have a huge query that is being made dynamically, but I want the
I have a project where I am taking some particularly ugly live HTML and
Not sure if this is possible or if I'm expressing correctly what I'm looking
I have some predicates being dynamically built that have the following signature passes through

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.