简体   繁体   中英

How to select two columns in one int array

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

在此处输入图片说明

But I need result as follows.

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

Thanks.

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();

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

尝试使用动态

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM