6
Answers

Delegates and LINQ

Prathap G

Prathap G

4y
712
1
Hi Folks,
 
We can Use Delegates in LINQ??
 
1. How can be used and what are the steps that need to be required. 
2. Wherever we can use for delegates???
 
Could you please help me with this. 
 
Regards,
Pratap
Answers (6)
4
Sachin Singh

Sachin Singh

11 55.8k 87.8k 4y
Delegate is something most developer don't understand or don't know its real power.
 
So lets start from english meaning of delegate :- (Represntative) that is the person who represents your country or team .Lets make it even simpler, a delegate is someone who acts as a messenger between two parties.An indian messenger can carry messages that india wants to deliver to America.
 
so where can you use it in programing:-
You have a task which is going to take lots of time so you will use thread or TPL ,now when the result will come a Messenger of the class (The Class who has long running task) will carry the Message and Hand it over to the other Classe's Method.Now the only constraint is the Consumer (Consumer method) must be of same signature.
 
I am not going to give a code example if you are a developer you must understand and try to implement it by your own later if you face problem you can ping us here.
 
Now the second meaning of Delegate :- I don't want to work on this project Lets delegate it to him.
How can you use this definition in programing.Microsoft has used this definition throughout in Asp.Net and in Linq.
in Linq you use Where clause ,isn't it? like----> numbers.Where(x=>x%2==0);  How are you able to use it Like this, only because Where takes Predicate delegate ,by doing that the Linq developer of MS team has give us the opportunity to write our own logic there ,meaning the Logic has been delegated to the Consumer.Anyone who will consume it will define the Logic.
Now again if you are a developer Decalare a delegate such that any one who will consume it would be free to define their Logic.
 
Credit :-  https://www.sharpencode.com/
 
2
Rajanikant Hawaldar

Rajanikant Hawaldar

32 38.8k 452k 4y
Yes we can, refer this LINQ video tutorial. https://www.youtube.com/playlist?list=PL4iLdIOlUy5IuGnp7NRdKHlB0PHZdS9EE
1
Ashutosh Gupta

Ashutosh Gupta

399 3.9k 1.1m 4y
delegates are nothing but function pointer in c#, it mainly used today for callback, filters, perdicate in linq etc.
 
Linq with collection where, First, FirstOrDefault etc clause where you want to get collection of items based on some filter and that filter is represented as delegate.  
 
e.g. you have list of items = new List { 10, 25, 36, 65 } and you want item which are greater than 30
then you can items.where(s => s > 30); or you want sepcific item with 25, Items.First(s => s == 25) 
 
 
 
1
Laxmidhar Sahoo

Laxmidhar Sahoo

NA 10.4k 55.4k 4y
 
Hi
@aashima is explained a better way ,So thanks
I put some more light here on Delegate in C#.
1 - It is as like as the function pointers in c++
2- Func delegate is a pointer to method that takes one or more parameters and must return a value
3- Func<> is a kind of Multicast Delegate used frequently with LINQ
static Func<int, int, float, double> add = delegate (int x, int y, float c) { return x + y + c; };
Console.WriteLine(add(2, 3, 5.5f)); // Outputs "10.5"
In lambda
Func<int, int, float, double> add = (a, b, c) => a + b + c;
//In case of function can take argument type any .Here can not take float then long
int[] ints = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
One of the overloaded versions of the LINQ Where operator accepts a Func delegate:
IEnumerable<T> Where<T>(this IEnumerable<T> source, Func<int, bool> predicate);
So we declare our Func:
Func<int, bool> GreaterThanTwo = i => i > 3;
Then we declare our query:
IEnumerable<int> intsGreaterThanTwo = ints.Where(GreaterThanTwo);
 
Thanks and regards 
 
1
Jignesh Kumar

Jignesh Kumar

30 39.5k 2.9m 4y
Hi,
 
Please refer these link which has deletegate example and step by step understanding, Inbuild delegates like,
Func, Action and Predicate, 
https://www.tutorialsteacher.com/csharp/csharp-func-delegate
https://www.c-sharpcorner.com/UploadFile/tirthacs/func-delegates-in-linq/ 
 
one of best example here which will give you clear idea,
https://www.youtube.com/watch?v=vBOzvNO8lvk 
1
Farhan Ahmed

Farhan Ahmed

64 28.3k 15.4m 4y
https://docs.microsoft.com/en-us/dotnet/standard/delegates-lambdas https://www.codingame.com/playgrounds/345/c-linq-background-topics/delegate-variables