Asp.netMVC模型

Model负责在View和控制器之间进行数据的传递:用户输入的内容封装为Model对象,发给Controller;要显示的数据有Controller放到Model中,然后扔给View去显示。Controller不直接和View进行交互。

查询id>1的狗有如下两种方法:

lambda写法:var r=dogs.where(d=>d.id>1)

Linq写法:var r2=from d in dogs   where d.id>1 select d;

Linq基本语法:以from item in items开始,items为待处理的集合,item为每一项的变量名;最后要加上select,表示结果的数据;记得select一定要放在最后

All---------Grouped

List<Product>products=GetProductList();

var productGroups=from p in products

                               group p by p.Category into g

                               where g.All(p=>p.UnitsInStock>0)

                              select new { Category =g.Key, Products = g };

Count-----Simple

获取列表中没有重复的元素数量

int []factors={ 2, 2, 3, 5, 5};

int unique=factors.Distinct().Count();

Console.WriteLine("There are {0} unique factors",unique);

Count---Nested

List<Customer> customers=GetCustomerList();

var orderCounts=from c in customers

                            select new{ c.CustomerID,OrderCount=c.Orders.Count() };

Count --------Grouped

List<Product>products=GetProductList();

var categoryCounts=from p in products

                                 group p by p.Category into g

                                 select new { Category=g.Key,ProductCount=g.Count() };

Sum----Grouped

List<Product>products=GetProductList();

var categories=from p in products

                         group p by p.Category into g

                         select new { Category=g.Key, TotalUnitsInstock=g.Sum(p=>p.UnitsInStock) };

ObjectDumper.Write(categories);

Min-----Projection

string[]words={"cherry","apple","blueberry"};

int shortestWord=words.Min(w=>w.Length);

Console.WriteLine("The shortest word is {0} characters long.",shortestWord);

Min-----Grouped

List<Product>products=GetProductList();

var categories=from p in products

                        group p by p.Category into g

                         select new { Category=g.Key , CheapestPrice=g.Min(p=>p.UnitPrice) };

ObjectDumper.Write(categories);

Min------Elements

List<Product>prodducts=GetProductList();

var categories=from p in products

                         group p by p.Category into g

                         let minPrice=g.Min(p=>p.UnitPrice)

                          select new { Category=g.Key,CheapestProducts=g.Where(p=>p.UnitPrice==minPrice) };

Max----Projection

string [ ] words={ "cherry", "apple", "blueberry" };

int longestLength=words.Max(w=>w.Length);

Console.WriteLine("The longest word is {0} characters long",longestLength);

Max-----Grouped

List<Product>products=GetProductList();

var categories=from p in products

                         group p by p.Category into g

                          select new {Category=g.Key,MostExpensivePrice=g.Max(p=>p.UnitPrice) };

Max------Elements

List<Product>products=GetProductList();

var categories=from p in products

                         group p by p.Category into g

                          let maxPrice=g.Max(p=>p.UnitPrice)

                          select new { Category=g.Key, MostExpensiveProducts=g.Where(p=>p.UnitPrice==maxPrice) };

ObjectDumper.Write(categories,1);

Average ---Projection

string[] words={"cherry", "apple", "blueberry"};

double averageLength=words.Average(w=>w.Length);

Console.WriteLine("The average word length is {0} characters.",averageLength);

Average----Grouped

List<Product>products=GetProductList();

var categories=from p in products

                         group p by p.Category into g

                          select new {Category=g.Key, AveragePrice=g.Average(p=>p.UnitPrice) };

Aggregate----Simple

这个例子是数组中所有的元素相乘,求出最终的积

double[] doubles={1.7, 2.3, 1.9, 4.1, 2.9};

double product=doubles.Aggregate((runningProduct,nextFactor)=>runningProduct*nextFactor);

Console.WriteLine("Total product of all numbers:{0}",product);

Concat----1

求两个集合的合集

int[] numbersA={0,2,4,5,6,8,9};

int[] numbersB={1,3,5,7,8};

var allNumbers=numbersA.Concat(numbersB);

Console.WriteLine("All numbers from both arrays:");

foreach(var n in allNumbers){

Console.WriteLine(n);

}

Concat---2

List<Customer>customers=GetCustomerList();

List<Product>products=GetProductList();

var customerNames=from c in customers

                                  select c.CompanyName;

var productNames=from p in products

                                select p.ProductName;

var allNames=customerNames.Concat(productNames);

Console.WriteLine("Customer and product names:");

foreach(var n in allNames){

Console.WriteLine(n);

}

SequenceEqual--1

var wordA=new string[]{ "cherry", "apple", "blueberry"};

var wordB=new string[]{"cherry", "apple", "blueberry"};

bool match=wordsA.SequenceEqual(wordsB);

Console.WriteLine("The sequences match: {0} ",match);

SequenceEqual--2

var wordsA=new string[]{"cherry", "apple", "blueberry"};

var wordsB=new string[]{"apple","blueberry","cherry"};

bool match=wordsA.SequenceEqual(wordsB);

Console.WriteLine("The sequences match:{0}", match);

List集合Group by 查询

var groupList=chRCheckConfirmList.GroupBy(x=>new {x.CompanyId, x.Type, x.ChRCheckItemConfigId}).select(group=>new { Keys=group.Key, TotalScore=group.Sum(p=>p.Score)})

原文地址:https://www.cnblogs.com/huanggenwei/p/10635332.html