0

假设我有一个包含项目的列表

-17" 屏幕
- 100GB 高清
-10788 火线- 锁
线
-显示器 - 鼠标 - 键盘 - USB


我想迭代从 A 到 c、D 到 F 等项目的列表...

I want this to run for items starting from A to items starting C
   @foreach (var item in Model.Items.OrderBy(i => i.Title))
            {
// Code
}

I want this to run for items starting from D to items starting F
   @foreach (var item in Model.Items.OrderBy(i => i.Title))
         {
// Code
}

任何帮助?

4

4 回答 4

4

我将首先定义一个视图模型(一如既往):

public class MyViewModel
{
    public string LetterRange { get; set; }
    public string[] Titles { get; set; }
}

然后是一个控制器动作,它将转换从某处获取模型,然后将其映射到视图模型。

备注:在这个例子中,我会将模型和视图模型之间的映射代码放在控制器动作中,但通常这应该放在单独的映射层中。例如,如果您使用AutoMapper,那可能是个好地方。

所以:

public ActionResult Index()
{
    // The model could be of any form and come from anywhere but
    // the important thing is that at the end of the day you will have
    // a list of titles here
    var model = new[] 
    { 
        "17\" Screen", 
        "100GB HD", 
        "10788 Firewire", 
        "Lock Cable", 
        "Monitor",
        "Mouse", 
        "Keyboard",
        "USB" 
    };

    // Now let's map this domain model into a view model 
    // that will be adapted to the requirements of our view.
    // And the requirements of this view is to group the titles
    // in ranges of 3 letters of the alphabet
    var viewModel = Enumerable
        .Range(65, 26)
        .Select((letter, index) => new 
        { 
            Letter = ((char)letter).ToString(), 
            Index = index 
        })
        .GroupBy(g => g.Index / 3)
        .Select(g => g.Select(x => x.Letter).ToArray())
        .Select(range => new MyViewModel
        {
            LetterRange = string.Format("{0}-{1}", range.First(), range.Last()),
            Titles = model
                .Where(item => item.Length > 0 && range.Contains(item.Substring(0, 1)))
                .ToArray()
        })
        .ToArray();

    // Let's add those titles that weren't starting with an alphabet letter
    var other = new MyViewModel
    {
        LetterRange = "Other",
        Titles = model.Where(item => !viewModel.Any(x => x.Titles.Contains(item))).ToArray()
    };

    // and merge them into the final view model
    viewModel = new[] { other }.Concat(viewModel).ToArray();

    return View(viewModel);
}

现在在相应视图中剩下的就是根据要求显示标题:

@model MyViewModel[]

@foreach (var item in Model)
{
    <h2>@item.LetterRange</h2>
    foreach (var title in item.Titles)
    {
        <div>@title</div>
    }
}

结果:

在此处输入图像描述


在将映射逻辑重构为映射层之后,相应的控制器操作可能如下所示:

public ActionResult Index()
{
    // The model could be of any form and come from anywhere but
    // the important thing is that at the end of the day you will have
    // a list of titles here
    DomainModel[] items = ...

    // Now let's map this domain model into a view model 
    // that will be adapted to the requirements of our view.
    var viewModel = Mapper.Map<IEnumerable<DomainModel>, IEnumerable<MyViewModel>>(items);

    return View(viewModel);
}

清洁和干燥。

于 2012-04-26T18:17:11.003 回答
1

为什么不:

var fromAtoC = Model.Items.Where(x => x.Title != null && x.Title[0] >= 'A' && x.Title[0] <= 'C');

foreach(Model.Items m in fromAtoC)
{
       //Do some stuff
}

var fromDtoF = Model.Items.Where(x => x.Title != null && x.Title[0] >= 'D' && x.Title[0] <= 'F');

for(Model.Items m in fromDtoF)
{
       //Do some stuff
}
于 2012-04-26T17:20:05.930 回答
0

不确定这是最优雅的解决方案,但它会起作用。我看看我能不能想出更好的东西...

var itemsAtoC = Model.Items.Where.Where(i => i.StartsWith("A") || i.StartsWith("B") || i.StartsWith("C"));

foreach(var item in itemsAtoC)

{
    Console.Write(item);
}

Ugggh 这也不是真正处理数字......

仍然在想...

于 2012-04-26T17:15:48.070 回答
0

尝试这个:

Model.Items.Sort((x, y)=> x.Title.CompareTo(y.Title));
foreach (var item in Model.Items.Where(x => x.Title[0] >= 'A' && x.Title[0] <= 'C')
{
  //code
}
于 2012-04-26T17:13:54.140 回答