101 LINQ Samples:Restriction Operators

本文提供了多个使用LINQ(Language Integrated Query)进行数据查询的示例,包括查找数组中小于5的元素,筛选缺货产品,查找库存中单价超过3.00的产品,筛选华盛顿州的客户及其订单,以及返回名称长度小于其数值的数字。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Where - Simple 1

This sample uses where to find all elements of an array less than 5.

public void Linq1(){    int[] numbers = { 5413986720 };     var lowNums =        from n in numbers        where n < 5        select n;     Console.WriteLine("Numbers < 5:");    foreach (var x in lowNums)    {        Console.WriteLine(x);    }}

Result

Numbers < 5: 




0

Where - Simple 2

This sample uses where to find all products that are out of stock.

public void Linq2(){    List<Product> products = GetProductList();     var soldOutProducts =        from p in products        where p.UnitsInStock == 0        select p;     Console.WriteLine("Sold out products:");    foreach (var product in soldOutProducts)    {        Console.WriteLine("{0} is sold out!", product.ProductName);    }}

Result

Sold out products: 
Chef Anton's Gumbo Mix is sold out! 
Alice Mutton is sold out! 
Thüringer Rostbratwurst is sold out! 
Gorgonzola Telino is sold out! 
Perth Pasties is sold out!

Where - Simple 3

This sample uses where to find all products that are in stock and cost more than 3.00 per unit.

public void Linq3(){    List<Product> products = GetProductList();     var expensiveInStockProducts =        from p in products        where p.UnitsInStock > 0 && p.UnitPrice > 3.00M        select p;     Console.WriteLine("In-stock products that cost more than 3.00:");    foreach (var product in expensiveInStockProducts)    {        Console.WriteLine("{0} is in stock and costs more than 3.00.", product.ProductName);    }}

Result

In-stock products that cost more than 3.00: 
Chai is in stock and costs more than 3.00. 
Chang is in stock and costs more than 3.00. 
Aniseed Syrup is in stock and costs more than 3.00. 
Chef Anton's Cajun Seasoning is in stock and costs more than 3.00. 
Grandma's Boysenberry Spread is in stock and costs more than 3.00. 
Uncle Bob's Organic Dried Pears is in stock and costs more than 3.00. 
Northwoods Cranberry Sauce is in stock and costs more than 3.00. 
Mishi Kobe Niku is in stock and costs more than 3.00. 
Ikura is in stock and costs more than 3.00. 
Queso Cabrales is in stock and costs more than 3.00. 
Queso Manchego La Pastora is in stock and costs more than 3.00. 
Konbu is in stock and costs more than 3.00. 
Tofu is in stock and costs more than 3.00. 
Genen Shouyu is in stock and costs more than 3.00. 
Pavlova is in stock and costs more than 3.00. 
Carnarvon Tigers is in stock and costs more than 3.00. 
Teatime Chocolate Biscuits is in stock and costs more than 3.00. 
Sir Rodney's Marmalade is in stock and costs more than 3.00. 
Sir Rodney's Scones is in stock and costs more than 3.00. 
Gustaf's Knäckebröd is in stock and costs more than 3.00. 
Tunnbröd is in stock and costs more than 3.00. 
Guaraná Fantástica is in stock and costs more than 3.00. 
NuNuCa Nuß-Nougat-Creme is in stock and costs more than 3.00. 
Gumbär Gummibärchen is in stock and costs more than 3.00. 
Schoggi Schokolade is in stock and costs more than 3.00. 
Rössle Sauerkraut is in stock and costs more than 3.00. 
Nord-Ost Matjeshering is in stock and costs more than 3.00. 
Mascarpone Fabioli is in stock and costs more than 3.00. 
Sasquatch Ale is in stock and costs more than 3.00. 
Steeleye Stout is in stock and costs more than 3.00. 
Inlagd Sill is in stock and costs more than 3.00. 
Gravad lax is in stock and costs more than 3.00. 
Côte de Blaye is in stock and costs more than 3.00. 
Chartreuse verte is in stock and costs more than 3.00. 
Boston Crab Meat is in stock and costs more than 3.00. 
Jack's New England Clam Chowder is in stock and costs more than 3.00. 
Singaporean Hokkien Fried Mee is in stock and costs more than 3.00. 
Ipoh Coffee is in stock and costs more than 3.00. 
Gula Malacca is in stock and costs more than 3.00. 
Rogede sild is in stock and costs more than 3.00. 
Spegesild is in stock and costs more than 3.00. 
Zaanse koeken is in stock and costs more than 3.00. 
Chocolade is in stock and costs more than 3.00. 
Maxilaku is in stock and costs more than 3.00. 
Valkoinen suklaa is in stock and costs more than 3.00. 
Manjimup Dried Apples is in stock and costs more than 3.00. 
Filo Mix is in stock and costs more than 3.00. 
Tourtière is in stock and costs more than 3.00. 
Pâté chinois is in stock and costs more than 3.00. 
Gnocchi di nonna Alice is in stock and costs more than 3.00. 
Ravioli Angelo is in stock and costs more than 3.00. 
Escargots de Bourgogne is in stock and costs more than 3.00.
Raclette Courdavault is in stock and costs more than 3.00. 
Camembert Pierrot is in stock and costs more than 3.00. 
Sirop d'érable is in stock and costs more than 3.00. 
Tarte au sucre is in stock and costs more than 3.00. 
Vegie-spread is in stock and costs more than 3.00. 
Wimmers gute Semmelknödel is in stock and costs more than 3.00. 
Louisiana Fiery Hot Pepper Sauce is in stock and costs more than 3.00. 
Louisiana Hot Spiced Okra is in stock and costs more than 3.00. 
Laughing Lumberjack Lager is in stock and costs more than 3.00. 
Scottish Longbreads is in stock and costs more than 3.00. 
Gudbrandsdalsost is in stock and costs more than 3.00. 
Outback Lager is in stock and costs more than 3.00. 
Flotemysost is in stock and costs more than 3.00. 
Mozzarella di Giovanni is in stock and costs more than 3.00. 
Röd Kaviar is in stock and costs more than 3.00. 
Longlife Tofu is in stock and costs more than 3.00. 
Rhönbräu Klosterbier is in stock and costs more than 3.00. 
Lakkalikööri is in stock and costs more than 3.00. 
Original Frankfurter grüne Soße is in stock and costs more than 3.00.

Where - Drilldown

This sample uses where to find all customers in Washington and then uses the resulting sequence to drill down into their orders.

public void Linq4(){    List<Customer> customers = GetCustomerList();     var waCustomers =        from c in customers        where c.Region == "WA"        select c;     Console.WriteLine("Customers from Washington and their orders:");    foreach (var customer in waCustomers)    {        Console.WriteLine("Customer {0}: {1}", customer.CustomerID, customer.CompanyName);        foreach (var order in customer.Orders)        {            Console.WriteLine("  Order {0}: {1}", order.OrderID, order.OrderDate);        }    }}

Result

Customers from Washington and their orders: 
Customer LAZYK: Lazy K Kountry Store

Order 10482: 3/21/1997 12:00:00 AM
Order 10545: 5/22/1997 12:00:00 AM 
Customer TRAIH: Trail's Head Gourmet Provisioners

Order 10574: 6/19/1997 12:00:00 AM
Order 10577: 6/23/1997 12:00:00 AM
Order 10822: 1/8/1998 12:00:00 AM 
Customer WHITC: White Clover Markets

Order 10269: 7/31/1996 12:00:00 AM
Order 10344: 11/1/1996 12:00:00 AM
Order 10469: 3/10/1997 12:00:00 AM
Order 10483: 3/24/1997 12:00:00 AM
Order 10504: 4/11/1997 12:00:00 AM
Order 10596: 7/11/1997 12:00:00 AM
Order 10693: 10/6/1997 12:00:00 AM
Order 10696: 10/8/1997 12:00:00 AM
Order 10723: 10/30/1997 12:00:00 AM
Order 10740: 11/13/1997 12:00:00 AM
Order 10861: 1/30/1998 12:00:00 AM
Order 10904: 2/24/1998 12:00:00 AM
Order 11032: 4/17/1998 12:00:00 AM
Order 11066: 5/1/1998 12:00:00 AM

Where - Indexed

This sample demonstrates an indexed Where clause that returns digits whose name is shorter than their value.

public void Linq5(){    string[] digits = { "zero""one""two""three""four""five""six""seven""eight""nine" };     var shortDigits = digits.Where((digit, index) => digit.Length < index);     Console.WriteLine("Short digits:");    foreach (var d in shortDigits)    {        Console.WriteLine("The word {0} is shorter than its value.", d);    }}

Result

Short digits: 
The word five is shorter than its value. 
The word six is shorter than its value. 
The word seven is shorter than its value. 
The word eight is shorter than its value. 
The word nine is shorter than its value.

转载于:https://www.cnblogs.com/zjz008/archive/2011/03/13/1982893.html

1. 用户与身体信息管理模块 用户信息管理: 注册登录:支持手机号 / 邮箱注册,密码加密存储,提供第三方快捷登录(模拟) 个人资料:记录基本信息(姓名、年龄、性别、身高、体重、职业) 健康目标:用户设置目标(如 “减重 5kg”“增肌”“维持健康”)及期望周期 身体状态跟踪: 体重记录:定期录入体重数据,生成体重变化曲线(折线图) 身体指标:记录 BMI(自动计算)、体脂率(可选)、基础代谢率(根据身高体重估算) 健康状况:用户可填写特殊情况(如糖尿病、过敏食物、素食偏好),系统据此调整推荐 2. 膳食记录与食物数据库模块 食物数据库: 基础信息:包含常见食物(如米饭、鸡蛋、牛肉)的名称、类别(主食 / 肉类 / 蔬菜等)、每份重量 营养成分:记录每 100g 食物的热量(kcal)、蛋白质、脂肪、碳水化合物、维生素、矿物质含量 数据库维护:管理员可添加新食物、更新营养数据,支持按名称 / 类别检索 膳食记录功能: 快速记录:用户选择食物、输入食用量(克 / 份),系统自动计算摄入的营养成分 餐次分类:按早餐 / 午餐 / 晚餐 / 加餐分类记录,支持上传餐食照片(可选) 批量操作:提供常见套餐模板(如 “三明治 + 牛奶”),一键添加到记录 历史记录:按日期查看过往膳食记录,支持编辑 / 删除错误记录 3. 营养分析模块 每日营养摄入分析: 核心指标计算:统计当日摄入的总热量、蛋白质 / 脂肪 / 碳水化合物占比(按每日推荐量对比) 微量营养素分析:检查维生素(如维生素 C、钙、铁)的摄入是否达标 平衡评估:生成 “营养平衡度” 评分(0-100 分),指出摄入过剩或不足的营养素 趋势分析: 周 / 月营养趋势:用折线图展示近 7 天 / 30 天的热量、三大营养素摄入变化 对比分析:将实际摄入与推荐量对比(如 “蛋白质摄入仅达到推荐量的 70%”) 目标达成率:针对健
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值