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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:38:44+00:00 2026-06-17T12:38:44+00:00

I have the following method on a MVC 4 Web API controller. public JsonResult

  • 0

I have the following method on a MVC 4 Web API controller.

public JsonResult GetJourney(List<string> assetIds, DateTime start, DateTime finish)
{
    var journey = new List<JourneyPoint>();

    var startUTC = start.ToUniversalTime();
    var finishUTC = finish.ToUniversalTime();

    foreach (var assetId in assetIds)
    {
        string id = assetId;

        var events = _eventRepo.GetAll().Where(evt => evt.EventTypeId == 0 && evt.TimeStamp > startUTC && evt.TimeStamp < finishUTC && evt.AssetId == id);

        foreach (var @event in events)
        {
            var myGps = _gpsRepo.GetAll().FirstOrDefault(gps => gps.Id == @event.GPSId);

            if (myGps != null)
            {
                var myJourneyPoint = new JourneyPoint
                {
                    Id = @event.Id,
                    AssetId = @event.AssetId,
                    TimeStamp = @event.TimeStamp.ToUnixEpocSeconds(),
                    Lat = myGps.Lat,
                    Long = myGps.Long,
                    Speed = myGps.Speed,
                    Elevation = myGps.Elevation,
                    Heading = myGps.Head
                };
                journey.Add(myJourneyPoint);
            }
        }
    }

    var jsonJourney = Json(journey.OrderBy(ju => ju.TimeStamp).ToList());
    jsonJourney.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
    return jsonJourney;
}

With the repo methods as:

public IQueryable<Event> GetAll()
{
    var db = new CasLogEntities();
    return db.Event;
}

public IQueryable<GPS> GetAll()
{
    var db = new CasLogEntities();
    return db.GPS;
}

This all works great, and the use of the repositories allowed me to write a test suite for the controller code.

Although I suspect that this code is inefficient, in that there are multiple calls to the data base and a lot of the computational work is being done by .Net and not the sql server.

Re-sharper suggested that I could turn the for each loops into linq statements, which i did and ended up with the following code.

public JsonResult GetJourney(List<string> assetIds, DateTime start, DateTime finish)
{
    var journey = new List<JourneyPoint>();

    var startUTC = start.ToUniversalTime();
    var finishUTC = finish.ToUniversalTime();

    foreach (var assetId in assetIds)
    {
        string id = assetId;

        var events = _eventRepo.GetAll().Where(evt => evt.EventTypeId == 0 && evt.TimeStamp > startUTC && evt.TimeStamp < finishUTC && evt.AssetId == id);

        journey.AddRange(from @event in events
                         let myGps = _gpsRepo.GetAll().FirstOrDefault(gps => gps.Id == @event.GPSId)
                         where myGps != null
                         select new JourneyPoint
                         {
                             Id = @event.Id,
                             AssetId = @event.AssetId,
                             TimeStamp = @event.TimeStamp.ToUnixEpocSeconds(),
                             Lat = myGps.Lat,
                             Long = myGps.Long,
                             Speed = myGps.Speed,
                             Elevation = myGps.Elevation,
                             Heading = myGps.Head
                         });
    }

    var jsonJourney = Json(journey.OrderBy(ju => ju.TimeStamp).ToList());
    jsonJourney.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
    return jsonJourney;
}

However I get an error when I run this code:

LINQ to Entities does not recognize the method ‘System.Linq.IQueryable`1[CasWeb.Models.DataContext.GPS] GetAll()’ method, and this method cannot be translated into a store expression.

I understand this is because LINQ To Entities is trying to map “GetAll()” method to sql and It can’t.

My question is: how can I rewrite my code to avoid this error and have as much work as possible performed by the SQL server? and if possible maintain the repository pattern to allow for testing?

  • 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-17T12:38:45+00:00Added an answer on June 17, 2026 at 12:38 pm

    As @Rup suggested in the comment, the following should avoid the error.

        journey.AddRange(from @event in events
                     join gps in _gpsRepo.GetAll() on gps.Id == @event.GPSId
                     select new JourneyPoint
                     {
                         Id = @event.Id,
                         AssetId = @event.AssetId,
                         TimeStamp = @event.TimeStamp.ToUnixEpocSeconds(),
                         Lat = gps.Lat,
                         Long = gps.Long,
                         Speed = gps.Speed,
                         Elevation = gps.Elevation,
                         Heading = gps.Head
                     });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following method: public static string UlList(this HtmlHelper helper, List<IEntity> entities, string
i have the following model method inside my asp.net MVc web application:- public IQueryable<User>
I have the following method: public string Phase(string phase) { return Phase 1; }
I have the following method: public static T ExecuteScalar<T>( string query, SqlConnection connection, params
I am using ASP.NET MVC 4 with Web Api I have the following ApiController.
I am using web api with ASP.NET MVC 4. I have the following named
I have a Spring MVC web application with conroller like below : @Controller public
On ASP.NET MVC 3, assume that we have following controller action: public ActionResult Index()
I have following method in wcf webenabled service Public Person AddPerson(Person p); As of
I have a MVC 4 web api application that receives json objects and uses

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.