Solved

I need to create an extension that completes Contains

Posted on 2011-02-16
5
270 Views
Last Modified: 2013-11-26
Hello,

   How can I create an extension to convert a string in many conditions in the Where part of a Linq. Look at this example:

   from x in data where x.name.MyFunction("john Mary")...

   My function should translate this to:

   from x in data where x.name.Contains("john") || x.name.Contains("Mary") ...

   I belive that the extension is like this, but I'm stuck!

  public MyFunction(string str)
  {
    foreach (var element in str.ToUpper().Split(" "))
    {
        ....            
    }
}

  Can someone help me?

  Thanks,
  Marco Castro
0
Comment
Question by:MarcoCastro
  • 3
5 Comments
 
LVL 11

Expert Comment

by:jasonduan
ID: 34906812
To create an extension method, you need create a static class.


public static class MyExtensionClass
{
     public static bool MyContains(this string str)
     {
        foreach (var element in str.ToUpper().Split(" "))
        {
            ....            
        }
    }
}



Note: "Contains" is already a method of string provided by .net framework, so you need use a different name other than "Contains"
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34906815
If you wanted to call your method "Include" then you could use:
    public static class Helpers
    {
        public static bool Includes(this string source, string value)
        {
            string[] values = value.Split(' ');
            foreach (string val in values)
                if (source.Contains(val))
                    return true;

            return false;
        }
    }

Open in new window

0
 

Author Comment

by:MarcoCastro
ID: 34907037
Carl,

  I need something like this. Your code don't works because the Linq Entity (I use FireBird) don't know how to transle this code to a SQL statement. Isn't possible to return a Linq Contains based function? Like this:

   data.Includes("a b")

   should returns

   data.Contains("a") || data.Contans("b") -- this code will de translated by the Linq Entity

   Thanks,
   Marco Castro
0
 

Accepted Solution

by:
MarcoCastro earned 0 total points
ID: 34991689
0
 

Author Closing Comment

by:MarcoCastro
ID: 35034601
I got the answer to my question by myself.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question