Solved

linq to find out whether primary phone is included

Posted on 2011-09-20
12
189 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Single Form 8 42
Simple Injector with Web Service 4 41
SSRS subreport with parameter 3 33
Reference issues in Visual Studio 3 30
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 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
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
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…

773 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