日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Linq的where语句中如果有两个条件以上的写法

發布時間:2023/12/20 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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语句中如果有两个条件以上的写法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。