1

I have a .Net Core project running and I am utilizing 2 different databases (A MySQL db and a PostGreSQL db). I have them set up and they are both implemented in my current controller - TripController.cs

TripController.cs

public IActionResult Index()
{
    var viewModels = new List<TripViewModel>();
    var Dids = _tripContext.Dids.ToList();
    foreach (var Did in Dids)
    {
        IQueryAble<Tripmetadata> trips = _tripContext.Tripmetadata.Where(t => t.Did == Did.Did);
        var tripsCount = trips.Count()
        //--------------------- I believe error is here ---------------------
        var alias = _context.Devices.Where(d => (long.Parse(d.Did)) == Did.Did).Select(d => d.Alias).ToString();
        // ------------------------------------------------------------------
        var viewModel = new TripViewModel
        {
            TripCount = tripsCount,
            didElement = Did,
            Alias = alias
        };
        viewModels.Add(viewModel)
    }

    return View(viewModels);
}

_context is the MySQL db and _tripContext is the PostGreSQL db.

Both database have a field called Did which I need to use. For the PostGreSQL db I need to use it to get the amount of trips (tripCount) for a given Did. However for the MySQL db I need to use the Did to get the alias for a device.

When I try to use the above code from the TripController to get the alias for a Did I get a weird value when displaying it in my view:

Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[System.String]

as seen here:enter image description here

I use a viewModel called TripViewModel which I pass to my View:

TripViewModel.cs

public class TripViewModel
{
    public int TripCount {get;set;}
    public long DidElement {get;set;}
    public string Alias {get;set;}
}

How do I get it write the right Alias in my view?

I have tried numerous methods that doesn't yield the result I need.

Zeliax
  • 4,987
  • 10
  • 51
  • 79

1 Answers1

2

I managed to find the solution.

My problem was not adding a .Single() at the end of my EF query.

I also found that this post is a duplicate. However I didn't know what my problem was until a stumpled upon this:

Zeliax
  • 4,987
  • 10
  • 51
  • 79