Michael Lam
asked on
linq to find out whether primary phone is included
supposed i have a list of objects (MyObject), and they each contain a phone number, phone type (work, home, etc), and IsPrimary. i need to find out that if a phone number(s) is given for a particular type, one of them needs to be primary (IsPrimary=1). however, if no phone number for a type is given, it's ok. can i do this using LINQ, or would a dictionary be a better way to go? thanks.
That sounds like a Where(x => x.IsPrimary == 1) clause in LINQ, unless I am missing something important...
ASKER
not quite that simple. if no phone number for a phone type exists, then there is no error. but if a phone number for a phone type exists, there must be 1 number that is primary. for example, if you have no phone numbers for you home phone type, it's fine. but if you have only 1 home phone number but it is not primary, then it's an error. on the other hand, if you have two home phone numbers, one of which is primary, then it's fine.
Are you talking about a 0..n multiplicity relationship between object and phone number?
ASKER
no, 1 to 1 relationship between object and phone number, in fact the object's name is PersonPhone. but you can have multiple PersonPhones, each with a phone number and have the same phone type, eg home phone.
That doesn't sound like a 1..1, "you can have multiple PersonPhones" means at least 1..n. Multiplicity means how many records do you expect to see on the child side.
If it is mandatory to have at least 1 PersonPhone record, you would have 1..n, otherwise it would be 0..n.
Examples:
0 phone records:
Object
1 phone record:
Object
+-- PersonPhone
2 phone records:
Object
+-- PersonPhone
+-- PersonPhone
If it is mandatory to have at least 1 PersonPhone record, you would have 1..n, otherwise it would be 0..n.
Examples:
0 phone records:
Object
1 phone record:
Object
+-- PersonPhone
2 phone records:
Object
+-- PersonPhone
+-- PersonPhone
ASKER
ok, i see what you mean, in that case it's 1...n. you need to have at least one primary phone number.
Is it an Object.PersonPhone property, with a type of List<PersonPhone>?
ASKER
yes.
I set up a small test structure to see if I could figure out what you need.
public class Object
{
public Object()
{
this.PersonPhoneList = new List<PersonPhone>();
}
public List<PersonPhone> PersonPhoneList { get; private set; }
public override string ToString()
{
return "Object: " + string.Join(",", this.PersonPhoneList.Select(x => x.Phone).ToArray());
}
}
public class PersonPhone
{
public string Phone { get; set; }
public int IsPrimary { get; set; }
}
List<Object> objectList = new List<Object>();
Object obj = new Object();
obj.PersonPhoneList.Add(new PersonPhone() { Phone = "313-555-1212", IsPrimary = 1 });
objectList.Add(obj);
obj = new Object();
obj.PersonPhoneList.Add(new PersonPhone() { Phone = "923-555-1212", IsPrimary = 1 });
obj.PersonPhoneList.Add(new PersonPhone() { Phone = "714-555-1212", IsPrimary = 0 });
objectList.Add(obj);
obj = new Object();
obj.PersonPhoneList.Add(new PersonPhone() { Phone = "311-555-1212", IsPrimary = 0 });
obj.PersonPhoneList.Add(new PersonPhone() { Phone = "555-555-1212", IsPrimary = 0 });
objectList.Add(obj);
obj = new Object();
objectList.Add(obj);
var query = from o in objectList
let p = o.PersonPhoneList
where p.Count == 0 ||
p.Where(x => x.IsPrimary == 1).FirstOrDefault() != null
select o;
List<Object> selectList = query.ToList();
Snapshot.png
ASKER
your PersonPhone class lacks a PhoneType property. again, if a particular phone type has any number, there must be a primary number. but if a particular phone type lacks any phone number, then it's acceptable.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Did you find a solution?