0

I am developing a software for managing members for a club.I created an offline search function that takes 3 parameters which are object attributes and returns a list of Members.

Member class has :

String name;
String email;
String telephone;

search method :

public static List<Member> Search(String name, String email, String telephone,String username)

The method uses at least one of the attributes to search. How can I make it work on 1 attribute , 2 , or 3 in case the user knows the name, email, and telephone of the Members. I was doing this :

if (name  != String.Empty) && email != String.Empty) && telephone != String.Empty))
{
   if (Member.FirstName.Equals(name) && Member.Email.Equals(email) && Member.Telephone.Equals(telephone))                            
       members_list.Add(Member.);
}
else if(name.Length > 0 && (email != String.Empty && telephone != String.Empty))
{
    ...
    ...
    ...etc
}

In sql it is quite easy

 WHERE (FirstName = @FirstName or @FirstName is null)
AND (Email = @Email or @Email is null)
AND (Username =@Username or @Username is null)
AND (Telephone =@Telephone or @Telephone is null)

But in offline mode, it is not that simple Any suggestions for a more efficient way ?

4
  • Why is your approach inefficient, does it work? Commented Aug 27, 2015 at 9:57
  • yes , but I think it is not smart enough , there should be a better algorithm, I need the method to search whether you give it one input , 2 , or 3 Commented Aug 27, 2015 at 9:57
  • You could make it more readable, instead of !(name.Equals(String.Empty) use name != "" Commented Aug 27, 2015 at 9:59
  • As the code is working rather post it on codereview.stackexchange.com Commented Aug 27, 2015 at 10:11

6 Answers 6

2

You can use LinQ to accomplish what you want. It has similiar to you SQL-like syntax:

public List<Member> Search(string name, string email, string telephone, string username, List<Member> source)
    {
        var query = from s in source
                    where (((s.name == name) || string.IsNullOrEmpty(name))
                        && ((s.email == email) || string.IsNullOrEmpty(email))
                        && ((s.telephone == telephone) || string.IsNullOrEmpty(telephone))
                        && ((s.username == username) || string.IsNullOrEmpty(username)))
                    select s;

        return query.ToList();
    }

    public class Member
    {
        public String username;
        public String name;
        public String email;
        public String telephone;
    }
Sign up to request clarification or add additional context in comments.

Comments

0
List<Member> members = GetAllMembers(); // collection of all members
var query = members.Select(m => m); // query without any filter conditions
if (!string.IsNullOrEmpty(name)) { // null or "" don't want filter
    query = query.Where(m => m.Name == name); // we want filter by name
}
if (!string.IsNullOrEmpty(email)) {
    query = query.Where(m => m.EMail == email); // we want filter by email
}
// ... other filter conditions
var result = query.ToList(); // collection of filtered members

Comments

0

My suggestion is to make a function for that, to reduce duplicate code and improve readibiliy.

The function name can be cheesy, but I can't resist it :)

private bool AllOrNothingEquals(string memberValue, string filterValue)
{
       return string.IsNullOrEmpty(filterValue) || memberValue.Equals(filterValue); 
}

And the usage (with a sprinkle of good practices for readibility):

bool validTelephone = AllOrNothingEquals(Member.Telephone, telephone);
bool validName = AllOrNothingEquals(Member.Name, name);
bool validEmail = AllOrNothingEquals(Member.Email, email);

if(validTelephone && validEmail && validName)
{
    //Is it valid
}
else
{
    //NotReally
}

Comments

0

Would this work:

if((string.IsNullOrEmpty(name) || Member.FirstName == name) && 
(string.IsNullOrEmpty(email) || Member.Email == email)) //etc
{
  // do stuff
} else {
  // do other stuff
}

Comments

0

Since you stated that you're performing an offline search, I understand you want to query an in-memory IEnumerable<T> like List<T> (i.e. you're using LINQ-to-Objects).

Actually there's a solution which simplifies the issue: don't accept the values, but a delegate which already specifies the criteria:

List<Member> members = new List<Member>();

public static List<Member> Search(Func<Member, bool> criteria)
{
    IEnumerable<Member> result = members.Where(criteria);

    // Do other stuff, manipulate results...

    return result;
}

Now simply call Search(member => member.FirstName == "Matias") or Search(member => member.FirstName == "Matias" && member.Email == "[email protected]") (or any other regular C# conditional!).

Comments

-1

Can you use that :

if((name != string.Empty && Member.FirstName.IndexOf(name) > -1) ||
    (email != string.Empty && Member.Email.IndexOf(email) > -1) ||
    (telephone!= string.Empty && Member.Telephone.IndexOf(telephone) > -1))
{
    //Found someone
}
else
{
    //Not found
}

In this code you search on the three parameters without being to rigid on the research algorythm. You can avoid one or more of the parameters.

2 Comments

Your solution will throw an exception if one or many Member properties are not initialized
You're right. You can add a Member.FirstName != null but I assume everything is already init.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.