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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:40:24+00:00 2026-05-28T13:40:24+00:00

Consider the following Query : var profilelst = ( from i in dbContext.ProspectProfiles where

  • 0

Consider the following Query :

var profilelst =
    (
        from i in dbContext.ProspectProfiles
        where i.CreateId == currentUser
        select new ProspectProfile
        {
            ProspectId = i.ProspectId,
            Live = i.Live,
            Name = i.Name,
            ServiceETA = i.Opportunities.OrderByDescending(t => t.FollowUpDate)
                .FirstOrDefault()
                .ServiceETA.ToString(),
            FollowUpDate = i.Opportunities.OrderByDescending(t => t.FollowUpDate)
                .FirstOrDefault()
                .FollowUpDate
        }
    )
    .ToList();

return profilelst.OrderByDescending(c=>c.FollowUpDate)
        .Skip(0).Take(endIndex)
        .ToList();

Here in this query please take a look at FollowUpDate and ServiceType, these both i have fetched from Opportunity table, is there any other work around to get these both..

One to Many Relationship in tables is like: ProspectProfile -> Opportunities

Whether the query i have written is ok or is there any another work around that can be done in easier way.

  • 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-05-28T13:40:25+00:00Added an answer on May 28, 2026 at 1:40 pm

    The only thing you can improve is to avoid ordering twice by changing your code to this:

    var profilelst
      = dbContext.ProspectProfiles
                 .Where(i => i.CreateId == currentUser)
                 .Select(i => 
                        {
                            var opportunity
                               = i.Opportunities
                                  .OrderByDescending(t => t.FollowUpDate)
                                  .First();
                            return new ProspectProfile
                            {
                                ProspectId = i.ProspectId,
                                Live = i.Live, 
                                Name = i.Name,
                                ServiceETA = opportunity.ServiceETA.ToString(),
                                FollowUpDate = opportunity.FollowUpDate
                            }
                        }).ToList();
    
    return profilelst.OrderByDescending(c => c.FollowUpDate).Take(endIndex).ToList();
    

    I made several changes to your original query:

    1. I changed it to use method chains syntax. It is just so much easier to read in my opinion.
    2. I removed the unnecessary Skip(0).
    3. The biggest change is in the Select part:
      1. I changed FirstOrDefault to First, because you are accessing the properties of the return value anyway. This will throw a descriptive exception if no opportunity exists. That’s better than what you had: In your case it would throw a NullReferenceException. That’s bad, NullReferenceExceptions always indicate a bug in your program and are not descriptive at all.
      2. I moved the part that selects the opportunity out of the initializer, so we need to do the sorting only once instead of twice.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following WSH snippet: var query = GetObject(winmgmts:).ExecQuery(SELECT Name FROM Win32_Printer, WQL, 0);
Consider the following criteria query: var x = SomeCriteria.AddOrder(new Order(Name, true)).List(); This will order
Consider the following query: SELECT operation, [...] FROM tableName The operation field will retrieve
Consider the following code: var Products_First = (from Entities.Product p in myContext.Product select p);
Consider the following SQL Server 2005/2008 query: Select [UID], [DESC] From SomeTable Order By
Consider the following JPQL query: SELECT foo FROM Foo foo INNER JOIN FETCH foo.bar
Consider following LINQ-to-NHibernate queries: var q1 = from se in query.ToList<SomeEntity>() where prop1 ==
Consider the following query: SELECT domain, done FROM tasks WHERE 1 GROUP BY domain
Please consider following sample query SELECT * FROM subscriber WHERE subscription_date>DATE(NOW()-INTERVAL 10 DAY) My
Consider the following 3 standard statements $queryString = SOME SQL SELECT QUERY; $queryResult =

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.