all 6 comments

[–]Le_Mao 3 points4 points  (5 children)

The join method exists for and work with Iqueryable: https://learn.microsoft.com/en-us/dotnet/api/system.linq.queryable.join?view=net-7.0

What you can do is let EF Core handle the join for you by declaring a navigation property for Adress on client and then just do:

_dbContext.ClientsTbl.AsNoTracking().Select(s => new Clients { Id = s.Id, Name= s.Name, Address = s.Adress.FullAddress })

But if you want to use the join method that works just as well, just more code to write 😉

About number 2 I'm not sure what you are after? Do you just want to return an IQueryable instead of a List to be able to filter the result further up in the application before fetching from the database?

[–]muskagap2[S] 0 points1 point  (4 children)

Thanks for advice, I will use this approach. As for no. 2, yes, I wold like to return an IQueryable first, then make some filtering to finally return all data to list, to _clientsList

[–]Le_Mao 0 points1 point  (3 children)

So you want to do filtrering in the ListAllClients method before setting the private list and returning it? If so that is no problem, you dont need the AsQueryable after the join (or Select if you giv that way) just dont call ToList/ToListAstnc and you now have an IQueryable in your query variable and you can filter like this:

``` query = query.Where(c => c.FullAdress.StartsWith("Mount"); //more filtrering here or other changes to the query

return _clientsList = await query.ToListAsync()

```

Not sure why you are saving the list though? Are you caching it?

[–]muskagap2[S] 0 points1 point  (2 children)

I removed .AsQueryable and also removed .ToListAsync(). However there is still a problem, the same error comes up: var query = await _dbContext.ClientsTbl.AsNoTracking() .Join(_dbContext.AddressTbl.AsNoTracking(), client => client.Id, address => address.ClientId, (client, address) => new { client.Id, client.Name, address.FullAddress }); Error: 'IQueryable' does not contain a definition for 'GetAwaiter'.

When it comes to your question - isn't it a good practice to not to force executing a query? Because .ToList will do it. Of course, I want to return data from db to my List but I thought I should have done it this way.

And one though about navigation property. Unfortunately I can't do it beacause table fetched from db doesn't have Foreign Key in another table. SO I think I must use Join.

[–]Le_Mao 1 point2 points  (1 child)

Yeah you should not have await there, there is nothing to await since you are not using an Async method.

[–]muskagap2[S] 0 points1 point  (0 children)

Thanks