-2

I want to select specific members whose phones start with "02". I have class Student with this constructor

public Student(string firstName, string lastName, int fn, int tel, string email, List<int> allMarks, int groupNumber)

Make a List<Student> students.

And that I want to do is to select this members that their telephone start with '02' (for my example)

I try this:

1st

var tel = students.Where(x => x.Tel.ToString().StartsWith("02")).ToArray();
Print(tel);

2nd

var testTel = students
.Select(x => x.Tel.ToString().Substring(0, 2) == "02")
.Select(x => x);
Print(testTel);

3rd

var someTel =
    from t in students
    where t.Tel.ToString().Substring(0, 2).Equals("02")
    select t.Tel;

And some more... but on the end I make (convert /int Tel to string Tel/) and work.

I want to ask is there a way to select this member when int Tel part of list:

List<Student> students = new List<Student>() 
    {
        new Student("Nataly", "Adams", 8222, 029669, "hot@mail.com", 
                new List<int>{2, 3, 3, 5, 6}, 2),
        new Student("Ben", "Dueyn", 8215, 0886996321, "hot@mail.com", 
                new List<int>{4, 5, 4, 5, 5}, 2),
    };
Hri100
  • 1
  • 1
  • 1
    no, because "int 00123" equals "int 123" – fubo Mar 26 '15 at 08:38
  • 2
    When you're storing the phone number as an `int`, you're losing your leading `0`. You should store them as `String`. – Phylogenesis Mar 26 '15 at 08:38
  • 2
    Making Tel as integer is a conceptual error. You cannot add or substract tel numbers ... It should be string by definition. Byt the way, for your problem, you need a ToString to using string comparison ... – cdie Mar 26 '15 at 08:39
  • Best way to store phone numbers http://stackoverflow.com/questions/3483156/whats-the-right-way-to-represent-phone-numbers – neildt Mar 26 '15 at 08:42
  • Thanks for advice. I am very grateful. Sorry for bad structure of the question and ...for the language – Hri100 Mar 26 '15 at 09:03

2 Answers2

2

Set your tel property as string, then you can simply use lambda expression like this:

var tel = students.Where(x => x.Tel.StartsWith("02")).ToList<Student>();

The answer why you should use string with telephone's number, you can find here - Link

Community
  • 1
  • 1
krypru
  • 1,692
  • 3
  • 22
  • 29
0

The most important bits and pieces are already said in the comments to your question. To iterate again:

  • 02 equals 2 when it comes to an Int32 value.
  • if you convert an int value with a leading 0 to a string, the leading zero gets lost.

The easisest way to get what you want, is to change your student class, so the phone number is of type String. As is Jon Skeets linked answer, this also enables you to store every possible special character like dashes, hyphens, parenthesis etc.

So change your class to this:

public class Student
{
    public Student(string fname, string lname, int fn, string tel, string mail, List<int> marks, int groupNumber)
    {
        this.firstName = fname;
        this.lastName = lname;
        this.fn = fn;
        this.tel = tel;
        //...
    }
    //...
    public string tel { get; set; }
    //...
}

And you can easily get what you want by using:

var result = students.Where(x => x.tel.StartsWith("02"));
Marco
  • 22,856
  • 9
  • 75
  • 124