Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

I need to create an extension that completes Contains

Posted on 2011-02-16
5
Medium Priority
?
329 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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.

636 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