Search code examples
c#linq-to-entities

Object values giving array of objects in linq


I'm trying to query in linq and the data is coming as expected but its coming as each object of my array is again coming as array of object so I'll have to select 0th object of perticular object every time , How to get the value in proper format. attaching my query... Thanks in advance

var results = (
    from site
    in db.Sites.Where(s => s.CompanyId == UserRecord.Company.CompanyId)
    join station
    in db.Stations.Include(y => y.Machines).Where(a => stationIds.Count() == 0 || stationIds.Contains(a.Id))
    on site.Id equals station.SiteId
    where ((noSearchParam || station.Name.ToLower().Contains(search.ToLower())) && (siteIds.Count() == 0 || siteIds.Contains(site.Id)))
    select station.Machines
).ToList();

Solution

  • You're currently using select station.Machines which I assume is a collection. You're selecting that for each station, so the result is a sequence of collections.

    You can flatten the results easily though. Either replace select station.Machines with

    from machine in station.Machines
    select machine
    

    Or keep your current query expression, and just change what you do afterwards to:

    .SelectMany(m => m).ToList();