Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

linq to find out whether primary phone is included

Posted on 2011-09-20
12
Medium Priority
?
209 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
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!

 

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 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month20 days, 17 hours left to enroll

810 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