Understand your C# queries! IEnumerable & IQueryable in explained

Understand your C# queries! IEnumerable & IQueryable in explained

tutorialsEU - C#

1 год назад

27,846 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Andy Robinson
Andy Robinson - 06.11.2023 20:06

Does anybody actually know what is going on with this stuff? We see heaps of examples everywhere but no informationon how anything actually works. Typical "aah" moment to be dashed again below by opposing views.

Ответить
K BHASKAR 17-312
K BHASKAR 17-312 - 05.11.2023 15:00

Thanks bro for vedios

Ответить
Kevin Vélez
Kevin Vélez - 24.10.2023 06:59

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
public class Program
{
static void Main(string[] args)
{
var db = new ORM();
IEnumerable<Customer> e = db.GetCustomerAsEnumerable();
IQueryable<Customer> q = db.GetCustomerAsQuery();

var highPay = from cust in e
where cust.Revenue < 2500
select cust;
highPay.ToList().ForEach(cust => { Console.WriteLine(cust.Revenue); });

var pay = from customer in q
where customer.Revenue < 2500
select customer;
pay.ToList().ForEach(cust => { Console.WriteLine(cust.Revenue); });

}
}

public class Customer
{
public int Revenue { get; set; }
public Customer(int revenue)
{
Revenue = revenue;
}
}

public class ORM
{
public List<Customer> CustomerList { get; set; } = new List<Customer>();
public ORM()
{
CustomerList.Add(new Customer(225));
CustomerList.Add(new Customer(45));
CustomerList.Add(new Customer(74));
CustomerList.Add(new Customer(435));
CustomerList.Add(new Customer(945));
CustomerList.Add(new Customer(346));
CustomerList.Add(new Customer(349));
CustomerList.Add(new Customer(984));
}

public IQueryable<Customer> GetCustomerAsQuery()
{
return CustomerList.AsQueryable();
}

public IEnumerable<Customer> GetCustomerAsEnumerable()
{
return CustomerList.AsEnumerable();
}

}

}

Ответить
odey Joshua Sunday
odey Joshua Sunday - 19.10.2023 14:03

beautiful, i enjoyed it...simple differences between the two

Ответить
Jacob Annooz
Jacob Annooz - 17.10.2023 21:21

easy explanation!

Ответить
Marcelo Milbradt
Marcelo Milbradt - 01.10.2023 21:58

This video just wrong; both IQueryable and IEnumerable will only execute the code when you iterate through the collection. Both implementations have the same impact on the database since the code is converted to SQL when using a ORM. Also, how can you claim that ICollection is faster without providing any data or benchmarks?

Ответить
Hiking Utah
Hiking Utah - 27.09.2023 00:45

IEnumerable<T> is an interface. So in your examples, the IEnumerable<T> also has 0 items--just like IQueryable. It's just an interface that you can use to iterate over the list of items (currently stored in your original List<T>). And you don't get the items until you iterate the collection.

Ответить
Yamen El-hasan
Yamen El-hasan - 29.08.2023 20:17

* support comment *

Ответить
Roberto De La Rosa Carbonell
Roberto De La Rosa Carbonell - 23.08.2023 16:59

Great explanation 👌

Ответить
MrMarfig
MrMarfig - 03.08.2023 17:56

Let's be clear here, the only reason why your example worked was that you decided to split the IEnumerable into two separate queries. Had you used the Where() extension method in the declaration of your enumerable object, and you would also have only returned 2500 customers through the network. In your example, there's actually no advantage in using one over the other. I think you can make a better video explaining the differences between IQueryable and IEnumerable if you focus on its actual benefits: With IQueryable, you can more easily compose queries dynamically based on runtime conditions. And you should avoid examples where you use IQueryable outside your ORM layer, unless you make it clear that doing so is actually not going to allow you to easily replace your ORM in the future.

Ответить
Fahreddin Artuklu
Fahreddin Artuklu - 30.07.2023 11:54

thank you bro i understand it and i think there is no missing part. :)

Ответить
Yussef
Yussef - 18.07.2023 01:01

IEnumerable is NOT a real collection

Ответить
HOW TO
HOW TO - 28.06.2023 22:38

Nice video understood totally.

Ответить
Ma-aruf Burad
Ma-aruf Burad - 22.06.2023 08:53

Well explained.

Ответить
EV
EV - 07.06.2023 15:12

Thank you for an amazing and easy explanation!

Ответить
Murtaza Chaudhary
Murtaza Chaudhary - 16.05.2023 23:59

Very well explained, using it from long time, but donot know the differences 😢

Ответить
Billy Arredondo
Billy Arredondo - 14.04.2023 11:24

Finally, I got it. Thank you so much!

Ответить
Simon Jones
Simon Jones - 28.03.2023 06:05

So helpful. Thanks a lot

Ответить
chella ack
chella ack - 12.03.2023 17:50

Awesome

Ответить