Solved

I need to create an extension that completes Contains

Posted on 2011-02-16
5
244 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 

Author Closing Comment

by:MarcoCastro
Comment Utility
I got the answer to my question by myself.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now