0

I was searching SO, but simply could not get the result for this (I was probably using incorrect keywords).

I have these multiple simple Linq expression:

var items = ErrorList.Where(n => n.CompanyID == SelectedItem.CompanyID);
var items = ErrorList.Where(n => n.CompanyID == SelectedItem.CompanyID && n.TaxCode == SelectedItem.TaxCode);

and so on (more conditions are added to this linq statement).

Instead of current method (using multiple if statements) I want to be able to specify my Function once and retrieve items based on specified criteria. I have not done this before, but I believe it should be something like this?

private System.Collections.IEnumerable Result(Func<>)

...I don't know how to continue

P.S. I don't need to create separate IEnumerable, if there is a way how to specify your function in advance? e.g.

                switch (sCase)
                    {
                    case 1:
                        sResult = (n => n.CompanyID == SelectedItem.CompanyID);
                        break;
                    case 2:
                        sResult = (n => n.CompanyID == SelectedItem.CompanyID && n.TaxCode == SelectedItem.TaxCode);
                        break;
                    }

                var items = ErrorList.Where(sResult);
Robert J.
  • 2,631
  • 8
  • 32
  • 59
  • I think you should read about [NHibernate](http://stackoverflow.com/a/3910604/3796048). This will enable you to Fetch, FetchMany options with the Query and QueryOver – Mohit S Sep 14 '15 at 08:10
  • 2
    It's not really clear what you're asking for. You talk about your "current method" but don't actually show us what that is. Perhaps doing so would be a good idea. – jmcilhinney Sep 14 '15 at 08:11
  • 1
    @Mohit: What does NHibernate have to do with LINQ? What makes you think that the OP is using NHibernate? How is NHibernate going to solve the OP's problem when they are using e.g. SQL Server and Entity Framework? Are we even sure we understand the question properly? – stakx - no longer contributing Sep 14 '15 at 08:12
  • I searched in the past for an answer, I dont think it is possible yet, there are some methods to help you achieve specific stuff, check out this for example - http://stackoverflow.com/questions/24575500/is-there-a-way-to-use-dynamic-in-lambda-expression-tree – Ziv Weissman Sep 14 '15 at 08:12
  • @Robert J.: **1.** re: _"more conditions are added to this linq statement"_. It would be important to know *how* more conditions are added. Can the conditions vary, are they e.g. provided by the user? Or did you simply abbreviate the code example for brevity, but the queried properties are always the same? **2.** It *might* also become relevant to know what the data source is (`ErrorList`). Is this an in-memory collection, or are you querying a database? – stakx - no longer contributing Sep 14 '15 at 08:14

0 Answers0