How does linq actually execute code to retrieve data from a data source?

I will start working on xamarin soon and pass a lot of code from the java java developer in C #.

In java, I use custom classes that are given argument conditions, etc., convert them to SQL statements and then load the results into project model objects

I'm not sure linq is the best option for filtering such data.

For example, what will happen now in these lines

List<Customer> customers = (new CustomerDAO()).get_all() 

Or if I have a condition

 List<Customer> customers = (new CustomerDAO()).get(new Condition(CustomerDAO.Code, equals, "code1") 

Now suppose I transferred classes to C # and I want to do something similar to the second case.

So, I will probably write something like:

 var customers = from customer in (new CustomerDAO()).get_all() where customer.code.equals("code1") select customer 

I know that the request will be executed only when I really try to contact clients, but if I have several calls to clients (let's say that I use 4 foreach loops later), will the get_all method be called 4 times? or results saved on first run?

Also, is it more efficient (in time because it is reasonable for memory, probably not) to just save the get_all () method and use linq to filter the results? Or use my existing installation, which actually performs

 Select * from Customers where code = 'code1' 

And loads the results into an object?

Thanks in advance for any help you can provide.

Edit: yes. I know that there is sqlite.net that pretty much does what my daos do, but probably better, and at some point I will probably convert all my objects to use it, I just need to know for the sake of knowing

+6
source share
1 answer

if I have multiple access to clients (let us say that I use 4 foreach loops later) will the get_all method be called 4 times? or results saved on first run?

Each time you enumerate a counter (using foreach in your example), the request will be re-executed if you do not save the materialized result somewhere. For example, if in the first request you run:

 var customerSource = new CustomerDAO(); List<Customer> customerSource.Where(customer => customer.Code.Equals("code1")).ToList(); 

Now you will work with the internal memory of List<Customer> without having to execute the query again.

Conversely, if every time you do:

 var filteredCustomers = customerSource.Where(customer => customer.Code.Equals("code1")) foreach (var customer in filteredCustomers) { // Do stuff } 

Then, for each listing, you will again execute the specified request.

Also, is it more efficient (reasonable, because memory wise probably not) just save the get_all () method and use linq to filter the results? Or use my existing installation, which actually performs

It really depends on your use case. Suppose you use LINQ for EF and the client table has a million rows, do you really want all of them to be in memory, and only then filter them out to use a subset of the data? Generally, it would be better to execute a fully filtered query.

+4
source

All Articles