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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:48:21+00:00 2026-06-06T19:48:21+00:00

I have a SQL query that returns a Datatable: var routesTable = _dbhelper.Select(SELECT [RouteId],[UserId],[SourceName],[CreationTime]

  • 0

I have a SQL query that returns a Datatable:

var routesTable = _dbhelper.Select("SELECT [RouteId],[UserId],[SourceName],[CreationTime] FROM [Routes] WHERE UserId=@UserId AND RouteId=@RouteId", inputParams);

and then we can work with Datatable object of routesTable

if (routesTable.Rows.Count == 1)
            {
                result = new Route(routeId)
                {
                    Name = (string)routesTable.Rows[0]["SourceName"],
                    Time = routesTable.Rows[0]["CreationTime"] is DBNull ? new DateTime() : Convert.ToDateTime(routesTable.Rows[0]["CreationTime"])
                };

                result.TrackPoints = GetTrackPointsForRoute(routeId);
            }

I want to change this code to linq but I don’t know how can I simulate Datatable in LINQ ,I wrote this part:

Route result = null;
            aspnetdbDataContext aspdb = new aspnetdbDataContext();
            var Result = from r in aspdb.RouteLinqs
                           where r.UserId == userId && r.RouteId==routeId
                           select r;


    ....

but I don’t know how can I change this part:

if (routesTable.Rows.Count == 1)
            {
                result = new Route(routeId)
                {
                    Name = (string)routesTable.Rows[0]["SourceName"],
                     Time = routesTable.Rows[0]["CreationTime"] is DBNull ? new DateTime() : Convert.ToDateTime(routesTable.Rows[0]["CreationTime"])
                };

would you please tell me how can I do this?

EDIT
here you can see the whole block of code in original

public Route GetById(int routeId, Guid userId)
        {
            Route result = null;
            var inputParams = new Dictionary<string, object>
                                  {
                                      {"UserId", userId},
                                      {"RouteId", routeId}
                                  };

            var routesTable = _dbhelper.Select("SELECT [RouteId],[UserId],[SourceName],[CreationTime] FROM [Routes] WHERE UserId=@UserId AND RouteId=@RouteId", inputParams);

            if (routesTable.Rows.Count == 1)
            {
                result = new Route(routeId)
                {
                    Name = (string)routesTable.Rows[0]["SourceName"],
                    Time = routesTable.Rows[0]["CreationTime"] is DBNull ? new DateTime() : Convert.ToDateTime(routesTable.Rows[0]["CreationTime"])
                };

                result.TrackPoints = GetTrackPointsForRoute(routeId);
            }

            return result;
        }

SELECT Function:

public DataTable Select(string query, Dictionary<string, object> parameters)
        {
            var dt = new DataTable();

            using (_command = new SqlCommand(query, _connnection))
            {
                InitializeParametersAndConnection(parameters);

                using (_adapter = new SqlDataAdapter(_command))
                {
                    _adapter.Fill(dt);
                }
            }

            return dt;
        }

and the GetTrackPointsForRoute

private List<TrackPoint> GetTrackPointsForRoute(int routeId)
        {
            aspnetdbDataContext aspdb = new aspnetdbDataContext();
            var result = new List<TrackPoint>();
            var trackPointsTable = from t in aspdb.TrackPointlinqs
                                   where t.RouteFK == routeId
                                   select t;
            foreach (var trackPointRow in trackPointsTable)
            {
                var trackPoint = new TrackPoint
                {
                    Id = (int)trackPointRow.TrackPointId,
                    Elevation = Convert.ToSingle(trackPointRow.Elevation),
                    Latitude = Convert.ToDouble(trackPointRow.Latitude),
                    Longitude = Convert.ToDouble(trackPointRow.Longitude),
                    Time = trackPointRow.TrackTime is DBNull ? new DateTime() : (DateTime)trackPointRow.TrackTime
                };
                result.Add(trackPoint);
            }

            return result;
        }
  • 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-06T19:48:23+00:00Added an answer on June 6, 2026 at 7:48 pm
    var firstRoute = aspdb.RouteLinqs
        .Where(r => r.UserId == userId && r.RouteId == routeId)
        .FirstOrDefault();
    
    if (firstRoute == null)
    {
        return null;
    }
    else
    {
        return new Route(routeId)
        {
            Name = first.SourceName,
            Time = first.CreationTime ?? new DateTime(),
            TrackPoints = GetTrackPointsForRoute(routeId)
        };
    }
    

    If this is LINQ to SQL you can simplify it further (this won’t work with LINQ to Entity Framework though):

    return aspdb.RouteLinqs
        .Where(r => r.UserId == userId && r.RouteId == routeId)
        .Select(r => new Route(routeId)
        {
            Name = r.SourceName,
            Time = r.CreationTime ?? new DateTime(),
            TrackPoints = GetTrackPointsForRoute(routeId)
        })
        .FirstOrDefault();
    

    Note: You probably can replace GetTrackPointsForRoute with a join to the child table, meaning that the entire method can be done with a single call to the database, rather than one call to get the routes, and a second call to get the points. To do this you should learn about associations and joins in LINQ to SQL.

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

Sidebar

Related Questions

I have a Microsoft SQL Server 2008 query that returns data from three tables
I have an sql query that returns rows from two tables with same column
I have a LINQ from SQL query that returns sorted rows. For instance, it
I have a sql query that I return into a DataTable: SELECT TABLE_NAME, COLUMN_NAME,
I have an SQL query that returns all companies records ignore the ones with
I currently have a SQL query that returns a number of fields. I need
I have a simple SQL query (using SqlCommand, SqlTransaction) in .NET 2.0 that returns
I'm using Sql-Server, and I have the below query which returns all columns that
I have a query that, basically, says this: SELECT DISTINCT [DB_ID] FROM [TableX] WHERE
I have a SQL query that returns the average time to signup for our

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.