Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6593209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:39:12+00:00 2026-05-25T17:39:12+00:00

Based upon a prior question I had Here . I wanted to join two

  • 0

Based upon a prior question I had Here. I wanted to join two collections together so that the merged data could be then displayed within a DataGrid of a Silverlight UI.

Using the Linq Zip function I was able to merge my two collections as follows

public void CombineAllCollections()
    {
       var combineAll = Persons.Zip(PhoneNumbers, (x, y) => new
        {
            FirstName = x.FirstName,
            LastName = x.LastName,
            Address = x.Address,
            State = x.State,
            Zip = x.Zip,
            AreaCode = y.AreaCode,
            PhoneNumber = y.Number
        });
      }

This seems to do just what I wanted. However the output is an Anonymous Type. How can I cast the combinedAll type to a collection (List, ObservableColl. IENum) that I can then pass to a view in my UI ( Bind to a datagrid) . The output should then display within the grid a column and value for each item ( as listed above seven columns).

Thanks in advance

-Cheers

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:39:13+00:00Added an answer on May 25, 2026 at 5:39 pm

    Instead of making an anonymous type, you could create a concrete type specifically for the merged results with all of the properties you specified on the anonymous type.

    For example:

    public void CombineAllCollections()
    {
       var combineAll = Persons.Zip(PhoneNumbers, (x, y) => new PersonWithPhoneNumber
        {
            FirstName = x.FirstName,
            LastName = x.LastName,
            Address = x.Address,
            State = x.State,
            Zip = x.Zip,
            AreaCode = y.AreaCode,
            PhoneNumber = y.Number
        });
     }
    

    Where PersonWithPhoneNumber is the type with all of the specified properties.

    Then you can call ToList() on the result to convert it to an IList<PersonWithPhoneNumber> or you can leave it in the form it is in as an IEnumerable<PersonWithPhoneNumber>

    EDIT

    Given the information that you provided, it would appear about the only effective way to store so many values while maintaining the ability to use LINQ and not have to define a separate type would be to create a dictionary for each item in the zipped collection, for example:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace Test
    {
        class Program
        {
            static void Main(string[] args)
            {
                var xs = new[] {1, 2, 3, 4, 5, 6};
                var ys = new[] {7, 8, 9, 10, 11, 12};
    
                var zipped = xs.Zip(ys, (x, y) =>
                                        new Dictionary<string, object>
                                            {
                                                { "X", x},
                                                { "Y", y}
                                            });
    
                foreach (var pair in zipped.SelectMany(tuple => tuple))
                {
                    Console.WriteLine("{0} = {1}", pair.Key, pair.Value);
                }
            }
        }
    }
    

    This way, you can have as many Key/Value pairs in the dictionary for each item as you would like. However, you essentially lose any type safety using this method and will end up boxing all value types for each property. This means you will have to know the type to cast back to. I would honestly not recommend doing this. I would simply just make a type, even if that type needs 100 properties. In this scenario auto-properties and a good text editor are your best friend.

    You could also us the new dynamic type in C# 4 with the .NET 4.0 framework as in the following example:

    using System;
    using System.Dynamic;
    using System.Linq;
    
    namespace Test
    {
        class Program
        {
            static void Main(string[] args)
            {
                var xs = new[] {1, 2, 3, 4, 5, 6};
                var ys = new[] {7, 8, 9, 10, 11, 12};
    
                var zipped = xs.Zip(ys, (x, y) =>
                                            {
                                                dynamic value = new ExpandoObject();
    
                                                value.X = x;
                                                value.Y = y;
    
                                                return value;
                                            });
    
                foreach (var pair in zipped)
                {
                    Console.WriteLine("X = {0}, Y = {1}", pair.X, pair.Y);
                }
            }
        }
    }
    

    This will introduce dynamic into your code, though. Which may not have the performance metrics you desire and can lead to a lot of run-time errors if you don’t keep an eye out on what you’re doing. This is because you could misspell a property while creating or accessing it (this is also an issue with the dictionary setup) which will lead to a run-time exception when you attempt to access it. Again, probably best to just use a concrete, named typed in the end.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to merger data together based upon a primary key that will be
I'm writing an app that displays some data which changes based upon the day
I am working on an asp.net web site that is based upon a single
I have some code in method C that will get executed based upon the
Based upon this question How to declarate LARGE_INTEGER in C# with answer of: [StructLayout(LayoutKind.Absolute,
I'm knocking together a demo app based upon Nancy.Demo.Authentication.Forms . I'm implementing Claims and
Increasingly I found myself using tools based upon python, particularly that use installation processes
I am implementing a code that based upon a particular response from a web
Question Is there a way to split up each class based upon a prefix
I have a database that can have different types of relatinships based upon what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.