0

I am trying to get records by their teamId for a group of players so far I have the following

public List<player> GetPlayersByTeamId(Guid teamId)
    {

        try
        {

            List<player> q = from _player in SoccerEntities.players.Where(a => a.teamId == teamId)
                             orderby _player
                             select _player;


            return q.ToList();

        }
        catch(Exception ex)
        {


        }

    }

But I am getting the following errror

Error 62 Cannot implicitly convert type 'System.Linq.IOrderedQueryable' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?) C:\new code\UniteCms\UniteCms\soccerCmsDal\SocerDataAccess.cs 299 42 soccerCmsDal

c-sharp-and-swiftui-devni
  • 3,743
  • 4
  • 39
  • 100

1 Answers1

2

Your linq expression is going to return an IQueryable not a List.

try this

 var q = from _player in SoccerEntities.players
                        Where a.teamId == teamId
                         orderby _player
                         select _player;

return q.ToList();

for future reference you can hover your cursor over the var keyword and it will tell you the inferred return type

and your are mixing query with method syntax. Probably would stick with one or the other.

mrsargent
  • 2,267
  • 3
  • 19
  • 36
  • mrsagent well what way would you prefare I right the query so that it returns a list suitable for grid data source ? – c-sharp-and-swiftui-devni Nov 09 '15 at 14:31
  • what im wanting is to work with live data set though so that i can delete objects from the grid will this work – c-sharp-and-swiftui-devni Nov 09 '15 at 14:36
  • You can still do a ToList() on the IQueryable to return a list. You just can't have the explicit type name of the query as a list. See my edited post. – mrsargent Nov 09 '15 at 15:19
  • but then I wont be able to get the live data item then sure i wont of a grid – c-sharp-and-swiftui-devni Nov 09 '15 at 15:21
  • You are going to want to use a ObservableCollection if you want to modify your collection and have it update the grid. So you will need to return an ObservableCollection instead of a List. The problem is you can't simply do a .ToObservableCollection() as you can with the .ToList(). But you can write an extension method like in this post. You will extend the IQueryable class instead of the IEnumerable class. http://stackoverflow.com/questions/9984594/iqueryablea-to-observablecollectiona-where-a-anonymous-type – mrsargent Nov 09 '15 at 15:51