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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:15:58+00:00 2026-05-18T09:15:58+00:00

I have this query for getting data through Entity Framework, which works today. Thing

  • 0

I have this query for getting data through Entity Framework, which works today. Thing is, i want to create the query dynamically, i.e build it up in blocks, but i can’t figure out how exactly to do it. I believe if i can somehow save one of the values that i retrieve in the beginning i can do it, but now i retrieve it twice (probably a bit slow as well? Unless the compiler fixes it before quering). Makes sense? Here is the LINQ:

from d in db.trains
where d.cancelled
&& d.station == toStation
&& d.date >= fromDate.Date
&& d.date <= toDate.Date
&& (from k in db.trains
    where 
    k.date == d.date
    && k.trainId == d.trainId
    && k.stationId == fromStation
    && k.position <= d.position
    select k.trainId).Contains(d.trainId)
select new
{
  trainId = d.trainId,
  date = d.date,
  arrival = d.arrival,
  departure = (from k in db.trains
               where
               k.date == d.date
               && k.trainId == d.trainId
               && k.stationId == fromStation
               && k.position <= d.position
               select k.departure).FirstOrDefault()
}
);

So you see, in order to get the departure, i have to retrieve the same thing again. So what i’m asking is can i save the object in the first query and then retrieve it later somehow? I can’t get the syntax working.

The database looks something like this:

trainId stationId date       arrival departure position

1       99        2010-10-11 10:00   10:10     1
1       98        2010-10-11 11:20   11:30     2
1       47        2010-10-11 12:30   12:40     3
2       99        2010-10-10 15:00   15:10     5

etc

So bascially, i need to retrieve two objects from the db, where the first one has stationId x and the other one has stationId y and both have the same date and trainId, and they have to be in the proper order, based on position (trains go both ways, but with different trainId’s).

Also, i would like to do be able to build this query dynamically, like this:

var trains = from d in db.trains
             select d;

if (id > 0)
  trains = trains.Where(p => p.trainId == id);

if (date != DateTime.MinValue)
  trains = trains.Where(p => p.date == date);

var items = (from train in trains).ToList();

I.e based on if various variables have values.

  • 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-18T09:15:58+00:00Added an answer on May 18, 2026 at 9:15 am

    You can use the let statement to store local variables and reuse them later
    Something like this:

    from d in db.trains
    where d.cancelled
    && d.station == toStation
    && d.date >= fromDate.Date
    && d.date <= toDate.Date
    let departures = (from k in db.trains
        where 
        k.date == d.date
        && k.trainId == d.trainId
        && k.stationId == fromStation
        && k.position <= d.position
        select k)
    where departures.Select(d => d.trainId).Contains(d.trainId)
    select new
    {
      trainId = d.trainId,
      date = d.date,
      arrival = d.arrival,
      departure = departures.Select(d => d.departure).FirstOrDefault()
    }
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query which retrieves Some data. I want to display that data
I have an issue getting data from three tables, which I want to return
I have this query in sql server 2000: select pwdencrypt('AAAA') which outputs an encrypted
I have this query statement and want to only get records that has a
i have this in my BlogRepository public IQueryable<Subnus.MVC.Data.Model.Post> GetPosts() { var query = from
I have this query in LINQ to Entities. var query = (from s in
I have this query: SELECT page.id, revision.title, revision.number FROM page INNER JOIN revision ON
I have this SQL query: SELECT * FROM IMAGES WHERE IMAGENAME in ('IMG1', 'IMG2',
I have this method in my db class public function query($queryString) { if (!$this->_connected)
This query is related to this one I asked yesterday. I have a radio

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.