1

I have a bool array that looks like this

bool[] responses = new bool[] {true, false, true};

And two classes that look like this:

public class Person {
  public IList<PersonDetail> PersonDetails
}

public class PersonDetail {
  public bool   Correct { get; set; }
}

PersonDetails
 >> PersonDetail[0].correct = true
 >> PersonDetail[1].correct = true
 >> PersonDetail[2].correct = false

Is there a simple way I can compare these to see if the true/false are equal for each? I was trying to use the c# .SequenceEqual but I don't know how to get the data from the PersonDetail class into a sequence xxx that I can use to compare with responses.

Here's what I have so far:

var equal = responses.Select(bool.Parse).SequenceEqual( xxx );

What I need to do is to compare the following:

responses[0] == PersonDetail[0].correct and
responses[1] == PersonDetail[1].correct and
responses[2] == PersonDetail[2].correct 

So what's true in responses[x] should match true in PersonDetail[x] and what's false in responses[x] should match false in PersonDetail[x]

3
  • A lil bit confusing here. Do you want to compare if the Person array have a repeated items? Commented Jun 15, 2011 at 8:14
  • Surely this is for-loop territory, I don't see how linq could do anything but slow it down? Commented Jun 15, 2011 at 8:19
  • 3
    "I have a string array that looks like this"... nope, it's a bool array. Commented Jun 15, 2011 at 8:23

8 Answers 8

1

Your code isn't really clear, but I imagine you want to do the following:

var isEqual = responses.SequenceEqual(PersonDetail.Select(p=>p.Correct))
Sign up to request clarification or add additional context in comments.

2 Comments

Cant the logical AND/OR operation be done, since they are bool type? I think thats much faster and efficient! :)
@zenwalker - I added some more to the question. Hope it explains it better. Thanks
1

You can use SequenceEqual like this

var responses = new [] { true, false, true };
var details = new List<PersonDetail>()
                { new PersonDetail() {Correct = true},
                  new PersonDetail() {Correct = false},
                  new PersonDetail() {Correct = true} };
var person = new Person() { PersonDetails = details };

var equal = responses.SequenceEqual(person.PersonDetails.Select(pd => pd.Correct));

Comments

1

From the answers already given I've had a play around and come up with the below, seems to work - note that I'm also new to C# so this has been an interesting problem.

As JonB points out in one of the answers - what happens if the lengths of "responses" and "people" are different... Hope the below helps

class Program
{       
    static void Main(string[] args)
    {
        bool[] responses = new bool[] { true, false, true };

        Person people = new Person();
        people.PersonDetails.Add(new PersonDetail() { Correct = true });
        people.PersonDetails.Add(new PersonDetail() { Correct = false });
        people.PersonDetails.Add(new PersonDetail() { Correct = true });

        bool equal = responses.SequenceEqual(people.PersonDetails.Select(P=> P.Correct));
        Console.WriteLine (equal);
    }
}

public class Person
{
    public List<PersonDetail> PersonDetails = new List<PersonDetail>();
}

public class PersonDetail
{
    public bool Correct;
}

Comments

1

If I'm reading your question correctly, this LINQ will do what you ask. I'm not sure it's more readable than a simple foreach loop but that's not the point!

public class Person 
{
  public IList<PersonDetail> PersonDetails;
}

public class PersonDetail 
{
  public bool   Correct;
}

void Main()
{
    bool[] responses = new bool[] {true, false, true};
    Person p = new Person();
    p.PersonDetails = new List<PersonDetail>();
    p.PersonDetails.Add(new PersonDetail(){Correct = true});
    p.PersonDetails.Add(new PersonDetail(){Correct = true});
    p.PersonDetails.Add(new PersonDetail(){Correct = false});

    //bool allGood = p.PersonDetails.Select((pd, index) => pd.Correct == responses[index]).All(x => x==true);

    bool allGood = responses.SequenceEqual(p.PersonDetails.Select(x => x.Correct));
    allGood.Dump(); // LINQpad extension
}

[edit - OK, so it's early and I forgot about SequenceEquals which does the same thing in a much more readable manner. Code changed to match what everyone else already answered...].

1 Comment

It's all relative - I'm not sure the LINQ is more readable than a for loop (if you don't know LINQ anyway) but the second LINQ is more readable than the first!
0

Not 100% sure of your code, but maybe:

var equal = responses.SequenceEqual(Person.PersonDetails.Select(PD => PD.correct);

4 Comments

I added some more to the question. Hope it explains it better. Thanks
I don't think this will work because I have the class Person that contains PersonDetail classes in a list called PersonDetails. The flag I want to compare responses to is a field in the PersonDetail class.
Thanks for the edit. When I run your suggestion I get an error message saying: An object reference is required for the non-static field, method or property Person.PersonDetails.get
@czhili, Replace Person with the name of your Person variable.
0

You could also try something like this:

IEnumerable<bool> answers = from x in Person.PersonDetails select x.correct;
bool equal = responses.SequenceEqual(answers)

Comments

0

Doesn't get much simpler than:-

  if (person.PersonDetails.Count != responses.Length)
    throw new ArgumentOutOfRangeException("Arrays are different lengths");
  bool result = true;
  for (int i = 0; i < person.PersonDetails.Count; i++)
  {
    if (person.PersonDetails[i].Correct != responses[i])
    {
      result = false;
      break;
    }
  }

Every programmer on the planet will be able to read it, even if they've never seen c#.

Actually it highlights a missing aspect of the spec; What should happen if the lists are different lengths?

6 Comments

If responses is shorter than person.PersonDetails however, you will get an error, so it does not address that aspect of the spec.
I'm not sure that "fancy" is really an objective in programming. There's a tendency towards spectacular flourishes when we're really after something that finishes quickly and can be read by a dog.
@Patrick I did mention that it highlights a missing aspect of the spec. Whilst it could return a value, should that be true or false? Since we don't know an error is valid.
Checked this out and it works. I think there is probably a one liner answer some place but this one does work :-) responses and person.PersonDetails are always the same length.
Ah sorry, I thought that you meant it addressed that aspect. It definitely raises the question, as SequenceEqual will just return false
|
0

Use XOR and not AND or OR operations on these items. XOR table will give you 0 if both items are the same.

Comments

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.