Linq的where语句中如果有两个条件以上的写法
using System;
using System.Linq;
using System.Collections.Generic;
namespace 外汇牌价抓取程序
{
public class 上海浦发
{
//上海浦发银行外汇牌价网址
private static string url = 网站地址.上海浦发.ToString();
/// <summary>
/// 抓取上海浦发银行外汇牌价信息
/// </summary>
/// <returns>信息列表</returns>
public 数据项实体[] 抓取()
{
//结果列表
List<数据项实体> list = new List<数据项实体>();
//获得页面的HTML形式的表示
System.Net.WebClient webClient = new System.Net.WebClient();
webClient.Encoding = System.Text.Encoding.UTF8;
string html = webClient.DownloadString(url);
SmartDocument smartTag = new SmartDocument(html);
List<SmartTag> allTags = new List<SmartTag>();
GetAllTags(smartTag, allTags);
//************有一个条件时*************//
var linqOne = from tag in allTags.ToArray()
where (tag.Name == "tr") && (tag.TagType == SmartTagType.Open) && (tag.ChildTags[0].FirstChild != null && (tag.ChildTags[0].FirstChild.Value.Substring(1, 2) == "美元" || tag.ChildTags[0].FirstChild.Value.Substring(1, 2) == "英镑" || tag.ChildTags[0].FirstChild.Value.Substring(1, 2) == "日元" || tag.ChildTags[0].FirstChild.Value.Substring(1, 2) == "欧元" || tag.ChildTags[0].FirstChild.Value.Substring(1, 2) == "香港"))
select new 数据项实体()
{
数据来源 = "上海浦发银行",
货币名称 = tag.ChildTags[0].FirstChild == null ? "" : tag.ChildTags[0].FirstChild.Value.Substring(1, 2),
现汇买入价 = Convert.ToDecimal(tag.ChildTags[4].FirstChild == null ? "0.0000" : tag.ChildTags[4].FirstChild.Value),
现钞买入价 = Convert.ToDecimal(tag.ChildTags[8].FirstChild == null ? "0.0000" : tag.ChildTags[8].FirstChild.Value),
卖出价 = Convert.ToDecimal(tag.ChildTags[10].FirstChild == null ? "0.0000" : tag.ChildTags[10].FirstChild.Value),
抓取日期 = DateTime.Now,
发布日期 = DateTime.Now
};
//************有二个条件时*************//
var linqTwo = from tag in allTags.ToArray()
where (tag.Name == "tr") && (tag.TagType == SmartTagType.Open) && (tag.ChildTags[0].FirstChild != null && (tag.ChildTags[0].FirstChild.Value.Substring(1, 2) == "美元" || tag.ChildTags[0].FirstChild.Value.Substring(1, 2) == "英镑" || tag.ChildTags[0].FirstChild.Value.Substring(1, 2) == "日元" || tag.ChildTags[0].FirstChild.Value.Substring(1, 2) == "欧元" || tag.ChildTags[0].FirstChild.Value.Substring(1, 2) == "香港"))
select tag.ChildTags[0].FirstChild.Value.Substring(1, 2) == "日元" ?
new 数据项实体()
{
数据来源 = "上海浦发银行",
货币名称 = tag.ChildTags[0].FirstChild == null ? "" : tag.ChildTags[0].FirstChild.Value.Substring(1, 2),
现汇买入价 = Convert.ToDecimal(tag.ChildTags[4].FirstChild == null ? "0.0000" : tag.ChildTags[4].FirstChild.Value) / 100,
现钞买入价 = Convert.ToDecimal(tag.ChildTags[8].FirstChild == null ? "0.0000" : tag.ChildTags[8].FirstChild.Value) / 100,
卖出价 = Convert.ToDecimal(tag.ChildTags[10].FirstChild == null ? "0.0000" : tag.ChildTags[10].FirstChild.Value) / 100,
抓取日期 = DateTime.Now,
发布日期 = DateTime.Now
}
: new 数据项实体()
{
数据来源 = "上海浦发银行",
货币名称 = tag.ChildTags[0].FirstChild == null ? "" : tag.ChildTags[0].FirstChild.Value.Substring(1, 2),
现汇买入价 = Convert.ToDecimal(tag.ChildTags[4].FirstChild == null ? "0.0000" : tag.ChildTags[4].FirstChild.Value),
现钞买入价 = Convert.ToDecimal(tag.ChildTags[8].FirstChild == null ? "0.0000" : tag.ChildTags[8].FirstChild.Value),
卖出价 = Convert.ToDecimal(tag.ChildTags[10].FirstChild == null ? "0.0000" : tag.ChildTags[10].FirstChild.Value),
抓取日期 = DateTime.Now,
发布日期 = DateTime.Now
};
//************有两个以上条件时*************//
var linq = from tag in allTags.ToArray()
where (tag.Name == "tr") && (tag.TagType == SmartTagType.Open) && (tag.ChildTags[0].FirstChild != null && (tag.ChildTags[0].FirstChild.Value.Substring(1, 2) == "美元" || tag.ChildTags[0].FirstChild.Value.Substring(1, 2) == "英镑" || tag.ChildTags[0].FirstChild.Value.Substring(1, 2) == "日元" || tag.ChildTags[0].FirstChild.Value.Substring(1, 2) == "欧元" || tag.ChildTags[0].FirstChild.Value.Substring(1, 2) == "香港"))
select (new Func<数据项实体>(delegate
{
if (tag.ChildTags[0].FirstChild.Value.Substring(1, 2) == "日元")
{
return new 数据项实体()
{
数据来源 = "上海浦发银行",
货币名称 = tag.ChildTags[0].FirstChild == null ? "" : tag.ChildTags[0].FirstChild.Value.Substring(1, 2),
现汇买入价 = Convert.ToDecimal(tag.ChildTags[4].FirstChild == null ? "0.0000" : tag.ChildTags[4].FirstChild.Value) / 100,
现钞买入价 = Convert.ToDecimal(tag.ChildTags[8].FirstChild == null ? "0.0000" : tag.ChildTags[8].FirstChild.Value) / 100,
卖出价 = Convert.ToDecimal(tag.ChildTags[10].FirstChild == null ? "0.0000" : tag.ChildTags[10].FirstChild.Value) / 100,
抓取日期 = DateTime.Now,
发布日期 = DateTime.Now
};
}
else if (tag.ChildTags[0].FirstChild.Value.Substring(1, 2) == "法元")
{
return new 数据项实体()
{
数据来源 = "上海浦发银行",
货币名称 = tag.ChildTags[0].FirstChild == null ? "" : tag.ChildTags[0].FirstChild.Value.Substring(1, 2),
现汇买入价 = Convert.ToDecimal(tag.ChildTags[4].FirstChild == null ? "0.0000" : tag.ChildTags[4].FirstChild.Value) / 100,
现钞买入价 = Convert.ToDecimal(tag.ChildTags[8].FirstChild == null ? "0.0000" : tag.ChildTags[8].FirstChild.Value) / 100,
卖出价 = Convert.ToDecimal(tag.ChildTags[10].FirstChild == null ? "0.0000" : tag.ChildTags[10].FirstChild.Value) / 100,
抓取日期 = DateTime.Now,
发布日期 = DateTime.Now
};
}
else
{
return new 数据项实体()
{
数据来源 = "上海浦发银行",
货币名称 = tag.ChildTags[0].FirstChild == null ? "" : tag.ChildTags[0].FirstChild.Value.Substring(1, 2),
现汇买入价 = Convert.ToDecimal(tag.ChildTags[4].FirstChild == null ? "0.0000" : tag.ChildTags[4].FirstChild.Value),
现钞买入价 = Convert.ToDecimal(tag.ChildTags[8].FirstChild == null ? "0.0000" : tag.ChildTags[8].FirstChild.Value),
卖出价 = Convert.ToDecimal(tag.ChildTags[10].FirstChild == null ? "0.0000" : tag.ChildTags[10].FirstChild.Value),
抓取日期 = DateTime.Now,
发布日期 = DateTime.Now
};
}
}))();
return linq.ToArray();
}
public 数据项实体 Get(SmartTag tag)
{
return new 数据项实体();
}
/// <summary>
/// 递归获取所有的当前DOM的标签
/// </summary>
/// <param name="smartTag">要遍历的标签</param>
/// <param name="allTags">当前标签的所有子标签</param>
private void GetAllTags(SmartTag smartTag, List<SmartTag> allTags)
{
allTags.Add(smartTag);
if (smartTag.HasChildTags == false)
{
return;
}
else
{
for (int i = 0; i < smartTag.ChildTags.Count; i++)
{
GetAllTags(smartTag.ChildTags[i], allTags);
}
}
}
}
}
转载于:https://www.cnblogs.com/SmartFramework/archive/2009/06/17/1505220.html
Linq的where语句中如果有两个条件以上的写法相关推荐
- java if and_关于java:if语句中可以有两个条件吗
我是Java编码的初学者,因此我会定期浏览以我的基本知识查找初学者项目. 最近,我正在创建一个聊天程序,用户可以在其中与我的计算机聊天. 这是代码的一部分,因为我不需要显示整个代码: System.o ...
- SQL语句中 Case具有两种格式:简单Case函数和Case搜索函数
转自:http://blog.csdn.net/IBM_hoojo/article/details/5546868 Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 ...
- c语言if语句条件是字母,C语言的if语句中,用作判断的条件表达式为()
语言的语句用作中国传统的为政之道一开始是诉诸于礼治的.() 在美国现役部队当中有10%的将军和20%的校官.判断尉官都是来自美国后备役军官训练团.() 司马光认为,条达式用人之术,与其用小人,不如用愚 ...
- if语句中同时判断多个条件的多种方法
总结一下自己经常用到的python中的if语句同时判断多个条件的不同方法,假设有: x, y, z = 0, 1, 0 方法一,多个逻辑运算符一起使用,这也是最常用的写法: if x == 1 or ...
- sql利用在一条语句中count出不同的条件的值
过多繁琐的sql影响代码质量,及维护成本,以下为两种小技巧处理方式,仅供参考,第二种更美观点 第一种,用case ---when---方法 select id ,sum(case when type ...
- LINQ to SQL语句(1)之Where(抄的好)
Where操作适用场景:实现过滤,查询等功能.说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子句.Where操作包括3种形式,分别为简单形式.关 ...
- LINQ to SQL语句 收藏系列
KB-Transaction in Linq to SQL http://blog.darkthread.net/post-2008-05-14-transaction-in-linq-to-sql ...
- Linq to Sql 语句全集
LINQ to SQL语句(1)之Where Where操作 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子 ...
- finereport报表设计中模板数据集的sql语句中if的用法_报表工具中动态参数的灵活运用...
报表开发过程中,有的时候我们会觉得普通参数很难满足一些业务需求,比如第二个数据集要引用第一个数据集的结果进行计算,动态控制 SQL 的过滤条件,动态列等,如果您遇到了这种情况,可以尝试使用动态参数即 ...
- sql语句中的子查询
一.子查询分类: 1.独立子查询:子查询语句可以独立查询 2.相关子查询: 独立子查询:子查询可以独立运行. 相关子查询: 子查询中引用了父查询的结果,不可以独立运行. 所有查询都可以用相关子查询实现 ...
最新文章
- 一般将来时语法课教案_「英语语法」一般过去时用法技巧全解
- FPGA逻辑设计回顾(13)RAM以及ROM的IP核定制以及关键参数
- FreeSWITCH异常原因总结
- delete archivelog all 无法彻底删除归档日志?
- 【转】 Nginx深入详解之多进程网络模型
- C语言:L1-035 情人节 (15分)(解题报告)
- Ajax updatepanel用法
- kotlin为什么比java编译慢_为什么Kotlin编译速度比Scala快? [关闭]
- 改变mysql的菜单栏的颜色_导航条——动态改变导航菜单的背景颜色
- Linux的实际操作:文件目录类的实用指令(rm mv)
- 计算机组成原理的实验节拍,一种计算机组成原理的实验装置
- pytorch macos_Windows,Linux和MacOS上的PyTorch安装
- shell脚本 空格
- vue 函数(二):callback回调函数
- 河南2002年计算机比赛,河南省体育局竞技体育管理信息系统(TYMIS2003)
- 关于数据迁移:解决kettle中mysql的数据是tinyint的kettle查出来后变成boolean问题
- 红包活动竟藏着这么多玩法(附使用技巧)
- hdu 1880 魔咒字典
- 智能制造专业就业方向
- 应用系统设计:预约挂号平台,B2C平台设计