9 Replies - 384 Views - Last Post: 24 June 2013 - 01:56 PM Rate Topic: -----

#1 ray1234  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 07-September 12

List<t> query question

Posted 24 June 2013 - 07:53 AM

Hi all,

I have a straight forward question, I have List<ivideo> which holds 5 video entries implementing ivideo.

I want to pull out all the objects that has a the value "bob" in either Title or Keyactor property.

public interface ivideo
{
  string Title {get; set;}
  string KeyActor {get; set;}
}




List<ivideo> list = new List<ivideo>();
list.?

Is This A Good Question/Topic? 0
  • +

Replies To: List<t> query question

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: List<t> query question

Posted 24 June 2013 - 07:59 AM

If you are using LINQ you can use the .Where method -

var bobs = list.Where(item => item.Title == "bob" || item.KeyActor == "bob");


Alternatively you can just loop through each item, do the tests on each item, if it matches add the item to another list. But personally I just like using LINQ :P
Was This Post Helpful? 1
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4464
  • View blog
  • Posts: 7,780
  • Joined: 08-June 10

Re: List<t> query question

Posted 24 June 2013 - 08:06 AM

I'd suggest using proper naming conventions as well. Interfaces should start with capital I. Your interface name should really be IVideo.

Also, if you want to modify Ryano121's query to handle partial searches, you can replace the equality comparison with a .Contains method call.
Was This Post Helpful? 1
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5479
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: List<t> query question

Posted 24 June 2013 - 08:17 AM

Also keep case-sensitivity in mind. "Bob" != "bob".
I generally down-case to lower then compare. So at this point I think all the suggestions combined become this:


var bobs = list.Where(item => item.Title.ToLower().Contains("bob") || item.KeyActor.ToLower().Contains("bob");

Was This Post Helpful? 0
  • +
  • -

#5 ray1234  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 07-September 12

Re: List<t> query question

Posted 24 June 2013 - 08:28 AM

this isn't from my actual code, I just typed it out on here focusing on the issue really. I'm aware of naming conventions

This post has been edited by ray1234: 24 June 2013 - 08:29 AM

Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4464
  • View blog
  • Posts: 7,780
  • Joined: 08-June 10

Re: List<t> query question

Posted 24 June 2013 - 08:32 AM

Acutally, I'd suggest using a SringComparison rather than doing a .ToLower.

Example:

if(string1.Equals(string2, StringComparison.OrdinalIgnoreCase)){
  //do something
}


There are a few comparison options. CurrentCulture, InvariantCulture, and Ordinal (and there's an IgnoreCase option for each). It's worth looking up how each behave.

Btw, these aren't just for equality, they can also be used for comparison, which is useful for sorting.

Here's an example of that:

static void Main(string[] args)
{
    string s1 = "abcdfghijklmnpqrstuvwxyz";
    var chars = s1.Select(x => x.ToString()).ToList();
    chars.Sort();
    PrintList(chars);
    chars.Sort(StringComparer.InvariantCulture);
    PrintList(chars);
    chars.Sort(StringComparer.Ordinal);
    PrintList(chars);
    Console.ReadKey();
}

private static void PrintList(List<string> chars)
{
    foreach (var c in chars) Console.Write(c);
    Console.WriteLine();
}



And more info:

http://msdn.microsof...ngComparer.aspx
Was This Post Helpful? 1
  • +
  • -

#7 ray1234  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 07-September 12

Re: List<t> query question

Posted 24 June 2013 - 08:33 AM

View PosttlhIn`toq, on 24 June 2013 - 03:17 PM, said:

Also keep case-sensitivity in mind. "Bob" != "bob".
I generally down-case to lower then compare. So at this point I think all the suggestions combined become this:


var bobs = list.Where(item => item.Title.ToLower().Contains("bob") || item.KeyActor.ToLower().Contains("bob");


what if I added more properties, say 8 more. it be tedious to do this for 8 properties. is there an easier way?
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5479
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: List<t> query question

Posted 24 June 2013 - 09:48 AM

If you want to avoid tedium, then pick another career field. MUCH of software development is tedious. So much of it is the little stuff that consumes hours: Like minor changes to GUI to make the boss happy... 25 different checks to stop the user from entering bad values or hurting themselves... test... testing... more testing...

Are there ways to avoid massive long queries? Sure. Play with it a little. This is where your 'engineering' skills come into play. Maybe a cascading set of checks that are only one item each. You get results for the title containing 'bob', send that subset to a check for the star containing 'moss', send that smaller subset to a check for a star containing 'trinity'. That's just one idea after 10 seconds of thought. I'm sure you can come up with some more methods if you put your mind to it. Ask 20 programmers how to do it and you will get 20 different techniques. That's as much about style as science. Programming is a rare cross between science and art. So put a little of yourself into it.
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4464
  • View blog
  • Posts: 7,780
  • Joined: 08-June 10

Re: List<t> query question

Posted 24 June 2013 - 01:46 PM

There's going to be the tedium of writing the comparisons once. But the smart way to handle that is to make it a method on the class that holds your values. For example:

public interface IVideo
{
  string Title {get; set;}
  string KeyActor {get; set;}
  bool Match(string text);
}

public class Video : IVideo
{
  string Title {get; set;}
  string KeyActor {get; set;}
  bool Match(string text)
  {
    return Title.Equals(text, StringComparison.CurrentCultureIgnoreCase)
      || KeyActor.Equals(text, StringComparison.CurrentCultureIgnoreCase);
  }
}

//somewhere else in your code:
var bobs = list.Where(v => v.Match("bob"));



So, you'll still have to write the comparison, but you can reuse the same code over and over again.
Was This Post Helpful? 1
  • +
  • -

#10 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5479
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: List<t> query question

Posted 24 June 2013 - 01:56 PM

Great example Curtis!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1