• 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

    • Mozilla really needed to focus on their core product for a while now. I will not mourn the death of pocket or AI garbage. One thing they don't do that I believe they should is advertise more, and not just to their core audience, especially their additional services. Let people know they actually exist.
    • Is this like tailscale?
    • I had no idea you could do this, thanks for the heads up!
    • Bonkers, really, when you think about the resources at Meta’s disposal.
    • You can now use Gemini in Google Forms to summarize responses by David Uzondu Over the last few months, Google has been aggressively pushing its Gemini AI into every corner of its Workspace apps, like generating entire documents from a prompt in Google Docs or creating fully editable charts in Sheets. Now, the company has set its sights on Google Forms. The latest update introduces a feature that uses Gemini to summarize text-based responses automatically. For any form with short-answer or paragraph questions, a new "Summarize responses" button will appear in the Responses tab once you collect more than three entries. Clicking it prompts Gemini to read the form's title, questions, and all the submitted text to spit out a summary of the key themes. Though Google bills this as a fresh expansion into Forms, we have actually had some Gemini intelligence in the app for a bit through the "Help me create a form" button. That feature, which drafts questions for you, was rolled out through the company's Workspace Labs program. For those unaware, Workspace Labs is basically Google's public testing ground, available in select countries, for new, and sometimes unproven, AI tools before a general release. Once a summary is generated, you can copy it to use elsewhere or hit "Retry" to see if Gemini comes up with a different take. If new responses roll in after you have generated a summary, you can click a "Refresh" button to update it with the latest data. For now, this feature is only available in English, and to use it, you'll need access to a paid Google Workspace plan, such as Business Standard, Business Plus, Enterprise Standard, or Enterprise Plus. It is also available to customers who pay for the Google AI Pro and Ultra plans or specific Gemini Education add-ons. The feature has started rolling out gradually, for Rapid Release domains, and will begin rolling out for Scheduled Release domains on June 26.
  • Recent Achievements

    • Week One Done
      Tech Dogs earned a badge
      Week One Done
    • Enthusiast
      computerdave91111 went up a rank
      Enthusiast
    • Week One Done
      Falisha Manpower earned a badge
      Week One Done
    • One Month Later
      elsa777 earned a badge
      One Month Later
    • Week One Done
      elsa777 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      531
    2. 2
      ATLien_0
      273
    3. 3
      +FloatingFatMan
      200
    4. 4
      +Edouard
      199
    5. 5
      snowy owl
      138
  • Tell a friend

    Love Neowin? Tell a friend!