English 中文(简体)
段 次 页 次 采用LINQ的较小名单
原标题:Split C# MultiSelectList into smaller ones using LINQ
  • 时间:2012-04-16 06:25:00
  •  标签:
  • c#
  • asp.net

Hi I have a MultiSelectList that I want to split into several smaller MultiSelectList, depending on the value (NOT the selectedValues), and I want to use LINQ to achieve that...sure it is possible but I can t cut it..any suggestions? Thanks.

最佳回答

这是你想要的:

var multiSelectList= new MultiSelectList(new List<string>()); //your mutli-select list
var multiSelectListGroupedByValue=ms.GroupBy(x => x.Value)
                                  .Select(x=>new MultiSelectList(x.Select(y=>y.Value)));
问题回答

采用该守则(从https://stackoverflow.com/questions/438188/split-a- Collection-into-n-parts-with-linq>)。 将收集材料带入“N”部分,并配有LINQ?

static class LinqExtensions
{
 public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> list, int parts)
 {
     int i = 0;
     var splits = from item in list
                  group item by i++ % parts into part
                  select part.AsEnumerable();
     return splits;
 }
}

我希望这是你们需要的。

@{
    var array = new int[] { 1,2,3,4,5,6,7,8,9,10 };
}

@Html.ListBox("lstBoxLessThan5",new MultiSelectList(array.Where(a => a < 5).ToArray()));
<br />
@Html.ListBox("lstBoxMorethan5",new MultiSelectList(array.Where(a => a > 5).ToArray()));

取代温室数据结构阵列的任何商业逻辑





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签