mirror of
https://github.com/rudollee/LearningMVC.git
synced 2025-06-08 00:16:14 +00:00
LINQ
This commit is contained in:
parent
63ae566736
commit
7868a902d7
@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Web;
|
using System.Web;
|
||||||
using System.Web.Mvc;
|
using System.Web.Mvc;
|
||||||
@ -91,5 +92,51 @@ namespace LanguageFeatures.Controllers
|
|||||||
|
|
||||||
return View("Result", (object)String.Format("Total: {0}", total));
|
return View("Result", (object)String.Format("Total: {0}", total));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ViewResult CreateAnonArray()
|
||||||
|
{
|
||||||
|
var oddsAndEnds = new[]
|
||||||
|
{
|
||||||
|
new {Name = "MVC", Category = "Pattern" },
|
||||||
|
new {Name = "Hat", Category = "Clothing" },
|
||||||
|
new {Name = "Apple", Category = "Frit" }
|
||||||
|
};
|
||||||
|
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
foreach (var item in oddsAndEnds)
|
||||||
|
{
|
||||||
|
result.Append(item.Name).Append(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
return View("Result", (object)result.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public ViewResult FindProduct()
|
||||||
|
{
|
||||||
|
Product[] products =
|
||||||
|
{
|
||||||
|
new Product {Name = "Kayak", Category = "Watersports", Price = 275M },
|
||||||
|
new Product {Name = "Lifejacket", Category = "Watersports", Price = 48.95M},
|
||||||
|
new Product {Name = "Soccer ball", Category = "Soccer", Price = 19.50M },
|
||||||
|
new Product {Name = "Coner flag", Category = "Soccer", Price = 34.95M }
|
||||||
|
};
|
||||||
|
|
||||||
|
var FoundProducts = from match in products
|
||||||
|
orderby match.Price descending
|
||||||
|
select new { match.Name, match.Price };
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
foreach (var p in FoundProducts)
|
||||||
|
{
|
||||||
|
result.AppendFormat("Price: {0} ", p.Price);
|
||||||
|
if (++count == 3)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return View("Result", (object)result.ToString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user