Solved

linq to find out whether primary phone is included

Posted on 2011-09-20
12
194 Views
Last Modified: 2012-08-14
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.
0
Comment
Question by:mmingfeilam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36569744
That sounds like a Where(x => x.IsPrimary == 1) clause in LINQ, unless I am missing something important...
0
 

Author Comment

by:mmingfeilam
ID: 36569887
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.  
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36569899
Are you talking about a 0..n multiplicity relationship between object and phone number?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mmingfeilam
ID: 36570084
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.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36570178
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
0
 

Author Comment

by:mmingfeilam
ID: 36570241
ok, i see what you mean, in that case it's 1...n.  you need to have at least one primary phone number.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36570265
Is it an Object.PersonPhone property, with a type of List<PersonPhone>?
0
 

Author Comment

by:mmingfeilam
ID: 36570319
yes.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36570543
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; }
}

Open in new window

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();

Open in new window

Snapshot.png
0
 

Author Comment

by:mmingfeilam
ID: 36715946
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.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 36717079
It might be better if you showed me some kind of class skeleton, like I did above, otherwise we will continue to go around and around...
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36818287
Did you find a solution?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Suggested Courses

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question