简体   繁体   English

如何使用LINQ将表列查询到数组中

[英]How to query a table column into an array with LINQ

I am just trying to use multiselect options in MVC by using ListBoxFor. 我只是想通过使用ListBoxFor在MVC中使用多选选项。 I created my viewmodel and passing that viewmodel to view. 我创建了我的视图模型并将该视图模型传递给视图。 But I found out that I need to create an array list to pass to view and when the results is posted back to controller I will be able to find out what has been selected and save those in the table. 但是我发现我需要创建一个数组列表以传递给视图,并将结果发布回控制器后,我将能够找出已选择的内容并将其保存在表中。 So I have a table called ArtistTypes and I would like to get the array list of artistTypeID from this table and pass it to view. 因此,我有一个名为ArtistTypes的表,我想从该表中获取artistTypeID的数组列表,并将其传递给视图。 I cannot figure it out how to query it to an array? 我不知道如何查询它到一个数组?

int[] selectedIds = _db.ArtistTypes.ToList().Select(x=> new int[]???);

用这个

int[] selectedIds = _db.ArtistTypes.Select(x=> x.artistTypeID).ToArray();

You can just accomplish by 您可以通过完成

int[] selectedIds = A.Select(x => x.ArtistId).ToArray();

But i will recommend you to use this 但是我会建议你使用这个

IEnumerable<int> selectedIds = A.Select(x => x.ArtistId);

because an Array also implement IEnumerable . 因为Array还实现IEnumerable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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