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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值