Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

linq to find out whether primary phone is included

Posted on 2011-09-20
12
Medium Priority
?
208 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
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
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

636 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