1

I have a data list and I want to make an array of two columns like-

var array= dataList.Select(a => a.CustomerId, a.EmployerId).ToArray();

Please suggest what is the correct way to make a array of two or more columns in entity framework.

Update

I also tried following.

var array= dataList.Select(a => new { a.CustomerId, a.EmployerId }).ToArray();

This gives result as follows

enter image description here

But I need result as follows.

[0] 5145
[1] 5155
[2] 5146
[3] 5149

Thanks.

1
  • 1
    var array= dataList.Select(a => new { a.CustomerId, a.EmployerId }).ToArray(); Commented Jan 24, 2017 at 12:09

3 Answers 3

2

Try this

var array = dataList.SelectMany(a => new int[] { a.EmployerId, a.CustomerId }).ToArray();

This will give you result as follows

[0] 5145
[1] 5155
[2] 5146
[3] 5149
Sign up to request clarification or add additional context in comments.

Comments

2

You should use Anonymous Types for this purpose:

var array= dataList.Select(a => new {a.CustomerId, a.EmployerId}).ToArray();

or as an another solution create a class:

public class Person
{
    public int CustomerId { get; set; }
    public int EmployerId { get; set; }
}

Then:

var array= dataList.Select(a => new Person{ CustomerId = a.CustomerId, EmployerId  = a.EmployerId}).ToArray();

And based on your EDIT you want to flatten the array, so you need to change the Select to SelectMany and also new to new[], like this:

var array = dataList.SelectMany(a => new int[] { a.CustomerId, a.EmployerId}).ToArray();

1 Comment

@AshokKumawat So you want to flatten the array, you need to change the Select to SelectMany and also new to new[].
0

try to use dynamic

var array= dataList.Select(a => new {  a.CustomerId, a.EmployerId }).ToArray();

1 Comment

That's not dynamic, that's an anonymous class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.