0

I am wondering if any one could help me, basically I am trying to flatten a list using linq. Inside the list is an array of phone numbers, What I can not figure out to do is count the size of the array and add each phone number as a unique list value.

var FlatenedList = from x in result
where x.ID != null
orderby x.ID

select new
   {
    AccountNumber = x.AccountNumber,
    Balance = x.Balance,
    BillToCompanyName = x.BillToContact.CompanyName,
    BillToName = x.BillToContact.Name,

    BillToPhoneNumber1 = x.BillToContact.PhoneNumbers[0].Number == null ? String.Empty : (x.BillToContact.PhoneNumbers[0].Number),
    BillToPhoneNumber2 = x.BillToContact.PhoneNumbers[1].Number == null ? String.Empty : (x.BillToContact.PhoneNumbers[1].Number),
    BillToPhoneNumber3 = x.BillToContact.PhoneNumbers[2].Number == null ? String.Empty : (x.BillToContact.PhoneNumbers[2].Number),       
    };
Jimbo Jones
  • 983
  • 4
  • 13
  • 48
  • 2
    Add all the phone numbers and get a unique list as in? Can you show some sample data? – Rahul Singh Jan 21 '15 at 13:13
  • What you have works. Do you mean you want this code to generate properties in your anonymous object until `BillToPhoneNumber99` if `BillToContact.PhoneNumbers` contains 100 entries? – CodeCaster Jan 21 '15 at 13:22

3 Answers3

1

If I have understood you correctly, this is what you need:-

var result = accounts.OrderBy(x => x.Id)
              .Select(x => new 
                {
                   AccountNumber = x.AccountNumber,
                   Balance = x.Balance,
                   BillToCompanyName = x.BillToContact.CompanyName,
                   BillToName = x.BillToContact.Name,
                   PhoneNumbersCount = x.BillToContact.PhoneNumbers.Count(),  
                                         //All phone numbers count including null
                   PhoneNumbersList = x.BillToContact.PhoneNumbers
                                       .Select(z => z.Number ?? String.Empty).ToList()
                });

You can check this Fiddle where I have used some sample data and let me know if this is what you were looking for.

Rahul Singh
  • 21,585
  • 6
  • 41
  • 56
0

May be you should try some thing like below,I haven't compiled it but this might work

var FlatenedList = from x in result
where x.ID != null
orderby x.ID

select new
   {
    AccountNumber = x.AccountNumber,
    Balance = x.Balance,
    BillToCompanyName = x.BillToContact.CompanyName,
    BillToName = x.BillToContact.Name,

    BillToPhoneNumber = x.BillToContact.PhoneNumbers.ToList().Where(num=>num.Number !=null).Select(num=>num) 
    };
0

I think you will need an Expand Object.

Here's how you can do it for one item:

var FlattenedItem = new ExpandoObject();

for(int i = 0; i < x.BillToContact.PhoneNumbers.length; i++) {
    // You will have to cast FlattenedItem to an IDictionary to add items
    ((IDictionary<string, object>)FlattenedItem).Add("BillToPhoneNumber" + i, x.BillToContact.PhoneNumbers[i]);
}

// This is your first phonenumber
var firstPhoneNumber = FlattenedItem.BillToPhoneNumber0;

Check this answer for more info: Creating an anonymous type dynamically?

Community
  • 1
  • 1
Vlemert
  • 316
  • 2
  • 5