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语句中如果有两个条件以上的写法的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 计算机二级excal试题,2016年计算
- 下一篇: 哈佛商学院20部必看电影
