• 0

Passing a delegate as a parameter (LINQ) ... C#


Question

Helloooooooo!!!!!! I'll keep this short. I have forgotten how it is called (and its syntax) when you pass a delegate as a parameter for doing the same query. I thought I had bookmarked the page where I saw that... :(

 

In english: 

 

Instead of doing something like this: 

public void SearchMethod(string value, int flag)
{
     if(flag == 1)
     {
          DBContext.Where(x => x.Contains(value));
     }

     else if(flag == 2)
     {
          DBContext.Where(x => x.Contains(value)).GroupBy(x => x.id)
     }
     else if(flag == 3)
    { 
          DBContext.Where(x => x.Contains(value)).GroupBy(x => x.Name)
    }
}

I would pass a Func<> as a paramter and I'll save myself from grabbing all those "if"s

public void SearchMethod(Func<string> query, int flag)
{
     //Do something with the query and save all those if
     // I don't remember the syntax used!
}

5 answers to this question

Recommended Posts

  • 0
  On 14/01/2015 at 14:33, SledgeNZ said:

I'll keep it shorter, look up predicates.

 

: D

Thanks :)Right now I'm not looking for predicates.

 

It's something similar to this answer:

http://codereview.stackexchange.com/a/5506

 

Edit:

Sorry!!!!

Yes, predicates are part of the equation :) I need to know how to do it with Func<> or Expression Trees...

  • 0

I don't understand what you're trying to do, but perhaps something like this (assuming DBContext contains DBItems and you actually want them back) ?

public IEnumerable<DBItem> SearchMethod<T>(string value, Func<DBItem, T> groupByFunc)
{
     var filtered = DBContext.Where(x => x.Contains(value));
     return groupByFunc == null
         ? filtered
         : filtered.GroupBy(groupByFunc);
}
It's not a big improvement, I've just bound the common case to a variable, encoded whether to use groupByFunc using a potentially null parameter and delegated the choice of the GroupBy function to the caller; either way the code does the same thing. But it's not clear what you're looking for.
  • 0

Thanks all! This is what I meant:

  Quote

 

t generally helps if you know the specific type of context that you are targeting.

For instance, let's say you are targeting a context that contains a collection of string objects and you wanted to search them. You would define a function like the following which accepted a function like so :

public void SearchMethod(Func<string,bool> query, int flag)
{
switch(flag)
{
case 1:
// Perform your search
DBContext.YourTable.Where(query);
break;
case 2:
DBContext.YourTable.Where(query).GroupBy(x => x.id);
break;
case 3:
DBContext.YourTable.Where(query).GroupBy(x => x.Name);
break;
}

}

Additionally, you could also pass in a function to handle your GroupBy query as well similar to the approach mentioned in this Stack Overflow response.

 

From:

forums.asp.net/p/2033405/5854959.aspx?p=True&t=635584716606703187

 

Thank you guys!!!! :D :D  Your answers gave me another idea for another part of my project!

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Ah so it's not in the .ISO it's self but it changes the definition in the OOBE update during setup?
    • They always have. This just updates what's included out of the box during the initial install.
    • So basically their prebuilding it into the ISOs?
    • Microsoft shares Defender anti-virus update for new Windows 11/10 ISOs by Sayan Sen Following the one for Lumma last month, Microsoft has also published a new Defender update for Windows 11/10/Server installation images this month. This update package is necessary as a Windows installation image may contain old, outdated anti-malware definitions and software binaries. Aside from better security, these updates can also provide improved performance benefits in some cases. When a new Windows installation is set up, there may be a temporary security risk due to outdated Microsoft Defender protection in the OS installation images. This happens because the antimalware software included in these images might not be up to date. Thus Microsoft says that these updated definitions essentially help close this protection gap. Microsoft delivered the latest security definitions for Windows images via security intelligence update version 1.431.54.0. The Defender package version is also the same. It applies to Windows 11, Windows 10 (Enterprise, Pro, and Home editions), Windows Server 2022, Windows Server 2019, and Windows Server 2016. Microsoft writes: From Microsoft's security bulletin, we learn that the security intelligence update version 1.431.54.0 was released this past week adds threat detections for various backdoor exploits, trojans, among others. For those wondering, the latest intelligence update is version 1.431.155.0 at the time of writing.
    • “In other news about OneDrive frustration, Microsoft recently locked out a user who was attempting to move a bunch of important data from old hard drives to OneDrive.” - It was probably CSMA..
  • Recent Achievements

    • One Month Later
      SekTheFirst earned a badge
      One Month Later
    • First Post
      zayanhani earned a badge
      First Post
    • First Post
      HarryTaylor earned a badge
      First Post
    • One Year In
      Eternal Tech earned a badge
      One Year In
    • One Month Later
      Eternal Tech earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      650
    2. 2
      ATLien_0
      232
    3. 3
      Michael Scrip
      224
    4. 4
      Steven P.
      141
    5. 5
      +FloatingFatMan
      140
  • Tell a friend

    Love Neowin? Tell a friend!