Lambda

Posted Lanny Priest

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Lambda相关的知识,希望对你有一定的参考价值。

Join:

context.SoftwareModelVersion.Join(context.ModelVersion.Where(m=>m.ModelName==toolStripTextBox1.Text) , q => q.ModelVersoinID, p => p.ModelVersionID, (q, p) => new { SoftwareModelVersionID = q.SoftwareModelVersionID })

 

OrderBy:

var reuslt = group.OrderBy(q=>q.Attribute);
 
var reuslt = group.OrderByDescending(q=>q.Attribute);
 连续排序:
var result = group.OrderByDescending(q=>q.Attribute).ThenBy(q=>q.Attribute2).ThenBy(q=>q.Attribute3);
 
GroupBy:
GroupBy之后通常需要输出具包含统计信息的匿名集合,那么可以这样:
objCollection.GroupBy(q=>q.GroupByKey)
.Select(q=>new {Attribute1 = q.First().GroupByKey, Attribute2 = q.Sum(q.Value));
 
Select,SelectMany:
Select返回集合
SelectMany将序列的每个元素投影到 IEnumerable(T) 并将结果序列合并为一个序列。
class PetOwner
{
    public string Name { get; set; }
    public List<String> Pets { get; set; }
}
 
PetOwner[] petOwners =
{
    new PetOwner { Name="Higa, Sidney",
        Pets = new List<string>{ "Scruffy", "Sam" } },
    new PetOwner { Name="Ashkenazi, Ronen",
        Pets = new List<string>{ "Walker", "Sugar" } },
    new PetOwner { Name="Price, Vernette",
        Pets = new List<string>{ "Scratches", "Diesel" } }
};
 
// Query using SelectMany().
var query1 = petOwners.Select(petOwner => petOwner.Pets).ToList();
//返回List<List<string>>;
var query2 = petOwners.SelectMany(petOwner => petOwner.Pets).ToList();
//返回合并后的List<string>;
 
where:
var reuslt = group.Where(q=>q.Attribute.Equal(compareObject));
 
Aggregate:
string[] stra = new string[] { "Able", "was", "I", "ere", "I", "saw", "Elba"};
string aa = stra.Aggregate("",(a, n) => a + " " + n);
输出:
"Able was I ere I saw Elba"

以上是关于Lambda的主要内容,如果未能解决你的问题,请参考以下文章

Lambda表达式

Lamda表达式

LinqToSQL3

lambda及stream学习

匿名函数lambda

运算符优先级