Code
/// <summary>
/// QuerySyntax(查询语法)的摘要说明
/// </summary>
public class QuerySyntax
{
public int ID { get; set; }
public string Name { get; set; }
public void QuerySyntaxTest()
{
List<QuerySyntax> list = new List<QuerySyntax>
{
new QuerySyntax { ID = 1, Name = "webabcd" },
new QuerySyntax { ID = 2, Name = "webabcde" },
new QuerySyntax { ID = 2, Name = "webabcdef" },
new QuerySyntax { ID = 2, Name = "webabcdefg" },
new QuerySyntax { ID = 2, Name = "webabcdefgh" },
new QuerySyntax { ID = 2, Name = "webabcdefghi" },
new QuerySyntax { ID = 2, Name = "webabcdefghij" },
new QuerySyntax { ID = 2, Name = "webabcdefghijk" },
new QuerySyntax { ID = 2, Name = "webabcdefghijkl" },
new QuerySyntax { ID = 2, Name = "webabcdefghijklm" },
new QuerySyntax { ID = 3, Name = "webabcdefghijklmn" }
};
IEnumerable<QuerySyntax> l = from o in list
where o.Name.Length > 10
orderby o.Name.Length descending
select o;
// 上面的(查询语法)等同于下面的(LINQ扩展方法和Lambda表达式)
// 查询语法相对更容易理解
// IEnumerable<QuerySyntax> l = list.Where(o => o.Name.Length > 10).OrderByDescending(o => o.Name.Length);
// Projection(映射)
// 可以返回一个新的类型
IEnumerable<Projection> l2 = from o in list
where o.Name.Length > 10
orderby o.Name.Length descending
select new Projection { Name = o.Name };
}
}
/// <summary>
/// 为了演示Projection(映射)而写的实体类
/// </summary>
public class Projection
{
public string Name { get; set; }
}
/// <summary>
/// QuerySyntax(查询语法)的摘要说明
/// </summary>
public class QuerySyntax
{
public int ID { get; set; }
public string Name { get; set; }
public void QuerySyntaxTest()
{
List<QuerySyntax> list = new List<QuerySyntax>
{
new QuerySyntax { ID = 1, Name = "webabcd" },
new QuerySyntax { ID = 2, Name = "webabcde" },
new QuerySyntax { ID = 2, Name = "webabcdef" },
new QuerySyntax { ID = 2, Name = "webabcdefg" },
new QuerySyntax { ID = 2, Name = "webabcdefgh" },
new QuerySyntax { ID = 2, Name = "webabcdefghi" },
new QuerySyntax { ID = 2, Name = "webabcdefghij" },
new QuerySyntax { ID = 2, Name = "webabcdefghijk" },
new QuerySyntax { ID = 2, Name = "webabcdefghijkl" },
new QuerySyntax { ID = 2, Name = "webabcdefghijklm" },
new QuerySyntax { ID = 3, Name = "webabcdefghijklmn" }
};
IEnumerable<QuerySyntax> l = from o in list
where o.Name.Length > 10
orderby o.Name.Length descending
select o;
// 上面的(查询语法)等同于下面的(LINQ扩展方法和Lambda表达式)
// 查询语法相对更容易理解
// IEnumerable<QuerySyntax> l = list.Where(o => o.Name.Length > 10).OrderByDescending(o => o.Name.Length);
// Projection(映射)
// 可以返回一个新的类型
IEnumerable<Projection> l2 = from o in list
where o.Name.Length > 10
orderby o.Name.Length descending
select new Projection { Name = o.Name };
}
}
/// <summary>
/// 为了演示Projection(映射)而写的实体类
/// </summary>
public class Projection
{
public string Name { get; set; }
}