Solved

linq to find out whether primary phone is included

Posted on 2011-09-20
12
185 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
  • 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now