Linq的where语句中如果有两个条件以上的写法
using System;
using System.Linq;
using System.Collections.Generic;
namespace 外匯牌價(jià)抓取程序
{
??? public class 上海浦發(fā)
??? {
??????? //上海浦發(fā)銀行外匯牌價(jià)網(wǎng)址
??????? private static string url = 網(wǎng)站地址.上海浦發(fā).ToString();
??????? /// <summary>
??????? /// 抓取上海浦發(fā)銀行外匯牌價(jià)信息
??????? /// </summary>
??????? /// <returns>信息列表</returns>
??????? public 數(shù)據(jù)項(xiàng)實(shí)體[] 抓取()
??????? {
??????????? //結(jié)果列表
??????????? List<數(shù)據(jù)項(xiàng)實(shí)體> list = new List<數(shù)據(jù)項(xiàng)實(shí)體>();
??????????? //獲得頁(yè)面的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);
???????????
???????????
??????????? //************有一個(gè)條件時(shí)*************//
??????????? 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 數(shù)據(jù)項(xiàng)實(shí)體()
????????????????????????? {
????????????????????????????? 數(shù)據(jù)來(lái)源 = "上海浦發(fā)銀行",
????????????????????????????? 貨幣名稱 = tag.ChildTags[0].FirstChild == null ? "" : tag.ChildTags[0].FirstChild.Value.Substring(1, 2),
????????????????????????????? 現(xiàn)匯買入價(jià) = Convert.ToDecimal(tag.ChildTags[4].FirstChild == null ? "0.0000" : tag.ChildTags[4].FirstChild.Value),
????????????????????????????? 現(xiàn)鈔買入價(jià) = Convert.ToDecimal(tag.ChildTags[8].FirstChild == null ? "0.0000" : tag.ChildTags[8].FirstChild.Value),
????????????????????????????? 賣出價(jià) = Convert.ToDecimal(tag.ChildTags[10].FirstChild == null ? "0.0000" : tag.ChildTags[10].FirstChild.Value),
????????????????????????????? 抓取日期 = DateTime.Now,
????????????????????????????? 發(fā)布日期 = DateTime.Now
????????????????????????? };
??????????? //************有二個(gè)條件時(shí)*************//
??????????? 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 數(shù)據(jù)項(xiàng)實(shí)體()
??????????????????????? {
??????????????????????????? 數(shù)據(jù)來(lái)源 = "上海浦發(fā)銀行",
??????????????????????????? 貨幣名稱 = tag.ChildTags[0].FirstChild == null ? "" : tag.ChildTags[0].FirstChild.Value.Substring(1, 2),
??????????????????????????? 現(xiàn)匯買入價(jià) = Convert.ToDecimal(tag.ChildTags[4].FirstChild == null ? "0.0000" : tag.ChildTags[4].FirstChild.Value) / 100,
??????????????????????????? 現(xiàn)鈔買入價(jià) = Convert.ToDecimal(tag.ChildTags[8].FirstChild == null ? "0.0000" : tag.ChildTags[8].FirstChild.Value) / 100,
??????????????????????????? 賣出價(jià) = Convert.ToDecimal(tag.ChildTags[10].FirstChild == null ? "0.0000" : tag.ChildTags[10].FirstChild.Value) / 100,
??????????????????????????? 抓取日期 = DateTime.Now,
??????????????????????????? 發(fā)布日期 = DateTime.Now
??????????????????????? }
??????????????????????? : new 數(shù)據(jù)項(xiàng)實(shí)體()
??????????????????????? {
??????????????????????????? 數(shù)據(jù)來(lái)源 = "上海浦發(fā)銀行",
??????????????????????????? 貨幣名稱 = tag.ChildTags[0].FirstChild == null ? "" : tag.ChildTags[0].FirstChild.Value.Substring(1, 2),
??????????????????????????? 現(xiàn)匯買入價(jià) = Convert.ToDecimal(tag.ChildTags[4].FirstChild == null ? "0.0000" : tag.ChildTags[4].FirstChild.Value),
??????????????????????????? 現(xiàn)鈔買入價(jià) = Convert.ToDecimal(tag.ChildTags[8].FirstChild == null ? "0.0000" : tag.ChildTags[8].FirstChild.Value),
??????????????????????????? 賣出價(jià) = Convert.ToDecimal(tag.ChildTags[10].FirstChild == null ? "0.0000" : tag.ChildTags[10].FirstChild.Value),
??????????????????????????? 抓取日期 = DateTime.Now,
??????????????????????????? 發(fā)布日期 = DateTime.Now
??????????????????????? };
??????????? //************有兩個(gè)以上條件時(shí)*************//
??????????? 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<數(shù)據(jù)項(xiàng)實(shí)體>(delegate
?????????????????????????? {
?????????????????????????????? if (tag.ChildTags[0].FirstChild.Value.Substring(1, 2) == "日元")
?????????????????????????????? {
?????????????????????????????????? return new 數(shù)據(jù)項(xiàng)實(shí)體()
?????????????????????????????????? {
?????????????????????????????????????? 數(shù)據(jù)來(lái)源 = "上海浦發(fā)銀行",
?????????????????????????????????????? 貨幣名稱 = tag.ChildTags[0].FirstChild == null ? "" : tag.ChildTags[0].FirstChild.Value.Substring(1, 2),
?????????????????????????????????????? 現(xiàn)匯買入價(jià) = Convert.ToDecimal(tag.ChildTags[4].FirstChild == null ? "0.0000" : tag.ChildTags[4].FirstChild.Value) / 100,
?????????????????????????????????????? 現(xiàn)鈔買入價(jià) = Convert.ToDecimal(tag.ChildTags[8].FirstChild == null ? "0.0000" : tag.ChildTags[8].FirstChild.Value) / 100,
?????????????????????????????????????? 賣出價(jià) = Convert.ToDecimal(tag.ChildTags[10].FirstChild == null ? "0.0000" : tag.ChildTags[10].FirstChild.Value) / 100,
?????????????????????????????????????? 抓取日期 = DateTime.Now,
?????????????????????????????????????? 發(fā)布日期 = DateTime.Now
?????????????????????????????????? };
?????????????????????????????? }
?????????????????????????????? else if (tag.ChildTags[0].FirstChild.Value.Substring(1, 2) == "法元")
?????????????????????????????? {
?????????????????????????????????? return new 數(shù)據(jù)項(xiàng)實(shí)體()
?????????????????????????????????? {
?????????????????????????????????????? 數(shù)據(jù)來(lái)源 = "上海浦發(fā)銀行",
?????????????????????????????????????? 貨幣名稱 = tag.ChildTags[0].FirstChild == null ? "" : tag.ChildTags[0].FirstChild.Value.Substring(1, 2),
?????????????????????????????????????? 現(xiàn)匯買入價(jià) = Convert.ToDecimal(tag.ChildTags[4].FirstChild == null ? "0.0000" : tag.ChildTags[4].FirstChild.Value) / 100,
?????????????????????????????????????? 現(xiàn)鈔買入價(jià) = Convert.ToDecimal(tag.ChildTags[8].FirstChild == null ? "0.0000" : tag.ChildTags[8].FirstChild.Value) / 100,
?????????????????????????????????????? 賣出價(jià) = Convert.ToDecimal(tag.ChildTags[10].FirstChild == null ? "0.0000" : tag.ChildTags[10].FirstChild.Value) / 100,
?????????????????????????????????????? 抓取日期 = DateTime.Now,
?????????????????????????????????????? 發(fā)布日期 = DateTime.Now
?????????????????????????????????? };
?????????????????????????????? }
?????????????????????????????? else
?????????????????????????????? {
?????????????????????????????????? return new 數(shù)據(jù)項(xiàng)實(shí)體()
?????????????????????????????????? {
?????????????????????????????????????? 數(shù)據(jù)來(lái)源 = "上海浦發(fā)銀行",
?????????????????????????????????????? 貨幣名稱 = tag.ChildTags[0].FirstChild == null ? "" : tag.ChildTags[0].FirstChild.Value.Substring(1, 2),
?????????????????????????????????????? 現(xiàn)匯買入價(jià) = Convert.ToDecimal(tag.ChildTags[4].FirstChild == null ? "0.0000" : tag.ChildTags[4].FirstChild.Value),
?????????????????????????????????????? 現(xiàn)鈔買入價(jià) = Convert.ToDecimal(tag.ChildTags[8].FirstChild == null ? "0.0000" : tag.ChildTags[8].FirstChild.Value),
?????????????????????????????????????? 賣出價(jià) = Convert.ToDecimal(tag.ChildTags[10].FirstChild == null ? "0.0000" : tag.ChildTags[10].FirstChild.Value),
?????????????????????????????????????? 抓取日期 = DateTime.Now,
?????????????????????????????????????? 發(fā)布日期 = DateTime.Now
?????????????????????????????????? };
?????????????????????????????? }
?????????????????????????? }))();
??????????? return linq.ToArray();
??????? }
??????? public 數(shù)據(jù)項(xiàng)實(shí)體 Get(SmartTag tag)
??????? {
??????????? return new 數(shù)據(jù)項(xiàng)實(shí)體();
??????? }
??????? /// <summary>
??????? /// 遞歸獲取所有的當(dāng)前DOM的標(biāo)簽
??????? /// </summary>
??????? /// <param name="smartTag">要遍歷的標(biāo)簽</param>
??????? /// <param name="allTags">當(dāng)前標(biāo)簽的所有子標(biāo)簽</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);
??????????????? }
??????????? }
??????? }
??? }
}
轉(zhuǎn)載于:https://www.cnblogs.com/SmartFramework/archive/2009/06/17/1505220.html
總結(jié)
以上是生活随笔為你收集整理的Linq的where语句中如果有两个条件以上的写法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 计算机二级excal试题,2016年计算
- 下一篇: 哈佛商学院20部必看电影