Csharp: Linq Query

来源:https://www.cnblogs.com/geovindu/archive/2020/05/15/12897598.html
-Advertisement-
Play Games

/// <summary> /// Linq 連接查詢 /// Geovin Du /// 塗聚文 /// https://docs.microsoft.com/en-us/dotnet/csharp/linq/perform-inner-joins /// </summary> /// <para ...


     /// <summary>
        /// Linq 連接查詢 
        /// Geovin Du
        /// 塗聚文
        /// https://docs.microsoft.com/en-us/dotnet/csharp/linq/perform-inner-joins
        /// </summary>
        /// <param name="confirmed"></param>
        /// <param name="deaths"></param>
        /// <param name="recovered"></param>
        /// <returns></returns>
        public List<HistoryList> getUnionNumber(List<History> confirmed, List<History> deaths, List<History> recovered)
        {
            List<HistoryList> list = new List<HistoryList>();


            if (recovered.Count > 0)
            {
                HistoryList newinfo = null;
                //1 種方法
                var newinfto = (from d in confirmed
                                join c in deaths on d.datename equals c.datename
                                join s in recovered on c.datename equals s.datename
                                select new
                                {
                                    datename = d.datename,//
                                    numberConfirmed = d.number,//
                                    numberDeaths = c.number, //
                                    numberRecovered = s.number  //
                                }).ToList();
                int k = 1;
                foreach (var ownerAndDu in newinfto)
                {
                    newinfo = new HistoryList();
                    newinfo.Id = k;
                    newinfo.datename = ownerAndDu.datename;
                    newinfo.numberConfirmed = ownerAndDu.numberConfirmed;
                    newinfo.numberDeaths = ownerAndDu.numberDeaths;
                    newinfo.numberRecovered = ownerAndDu.numberRecovered;
                    k++;
                    list.Add(newinfo);
                }
               
            }
            else
            {
                HistoryList newinfo = null;
                //1 種方法
                var newinfto = from d in confirmed
                               join c in deaths on d.datename equals c.datename
                               select new
                               {
                                   datename = d.datename,
                                   numberConfirmed = d.number,
                                   numberDeaths = c.number
                            };
                int k = 1;   
                foreach (var ownerAndDu in newinfto)
                {
                    newinfo = new HistoryList();
                    newinfo.Id = k;
                    newinfo.datename = ownerAndDu.datename;
                    newinfo.numberConfirmed = ownerAndDu.numberConfirmed;
                    newinfo.numberDeaths = ownerAndDu.numberDeaths;
                    k++;
                    list.Add(newinfo);
                }

            }

#region 
            //for (int i = 0; i < confirmed.Count; i++)
            //{

            //    HistoryList newinfo = null;
            //    if (recovered.Count > 0)
            //    {                   
        
            //        //var ss1 =confirmed.Join(deaths, p => p.datename, r => r.datename, (p, r) => p).OrderByDescending(p => p.datename).ToList();
            //        //2 種方法
            //        if (confirmed[i].datename == deaths[i].datename && confirmed[i].datename == recovered[i].datename)
            //        {
            //            newinfo = new HistoryList();
            //            newinfo.datename = confirmed[i].datename;
            //            newinfo.numberConfirmed = confirmed[i].number;
            //            newinfo.numberDeaths = deaths[i].number;
            //            newinfo.numberRecovered = recovered[i].number;
            //            list.Add(newinfo);
            //        }                   
            //    }
            //    else
            //    {    
            //        //2 種方法
            //        if (confirmed[i].datename == deaths[i].datename)
            //        {
            //            newinfo = new HistoryList();
            //            newinfo.datename = confirmed[i].datename;
            //            newinfo.numberConfirmed = confirmed[i].number;
            //            newinfo.numberDeaths = deaths[i].number;
            //            list.Add(newinfo);
            //        }
            //    }
#endregion 

            
            return list;

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    id = Request.QueryString["Id"];
                    type = Request.QueryString["type"];
                    country = Request.QueryString["country"];
                    province = Request.QueryString["province"];
                    Response.Write("Value:"+id + "," + type+","+country+","+province);
                    change(int.Parse(type));
                    List<Location> lodea = col.deaths.locations.Where(x => x.country == country && x.province==province).ToList();
                    //List<Location> lodea = dea.locations.Where(x => x.country == "US").ToList();
                    //List<Location> locir = conf.locations.Where(x => x.country == "US").ToList();
                    List<Location> locir = col.confirmed.locations.Where(x => x.country == country && x.province == province).ToList();
                    // List<Location> lorec = recov.locations.Where(x => x.country == "US").ToList();
                    List<Location> lorec = col.recovered.locations.Where(x => x.country == country && x.province == province).ToList();
                    //in
                    var geovindu = from geovi in col.recovered.locations
                                   where (new string[] { "china", "chile", "Canada" }).Contains(geovi.country)
                                   select geovi;

                    List<History> hidea = lodea[0].history;  // dea.locations.Where(x => x.country == "Yemen").ToList().Where(x => x.history.All).ToList();
                    List<History> hicor = locir[0].history;
                    List<History> hirec=new List<History>();
                    if(lorec.Count>0)
                    {
                       hirec = lorec[0].history;
                    }
                    

                    List<HistoryList> uniolist = new List<HistoryList>();
                    uniolist = getUnionNumber(hicor, hidea, hirec);
                    //分頁查詢
                    var geovin = (from du in uniolist
                                  where du.Id > 10
                                  orderby du.Id descending
                                  select du).Skip(10).Take(10); //取第11條到第20條數據             

                    this.GridView1.DataSource = uniolist;
                    this.GridView1.DataBind();


                }

            }
            catch (Exception ex)
            {
                Response.Write(ex.Message.ToString());
            }
        }
    }

  數據來源測試: https://coronavirus-tracker-api.herokuapp.com/all


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 這有一個專註Gopher技術成長的開源項目 "「go home」" 背景 作為一名程式員,家裡多多少少會有一些落了灰的電腦,如果把閑置的電腦變成伺服器,不僅有良好的配置,還能用來做各種測試,那就再好不過了。但是區域網的設備怎麼被外網訪問呢?這就靠內網穿透來實現了。 內網穿透又叫 "NAT" 穿透,常 ...
  • ReentrantLock完美實現了互斥,完美解決了併發問題。但是卻意外發現它對於讀多寫少的場景效率實在不行。此時ReentrantReadWriteLock來救場了!一種適用於讀多寫少場景的鎖,可以大幅度提升併發效率,你必須會哦! 序幕 為何引入讀寫鎖? ReentrantReadWriteLoc ...
  • 接上一章, "Windows玩轉Kubernetes系列1 VirtualBox安裝Centos" ,我們開始學習如何在Centos中安裝Docker 準備 關閉防火牆 防火牆一定要提前關閉,否則在後續安裝K8S集群的時候,會有一些問題,執行下麵語句: 關閉Swap 註掉swap 關閉SeLinux ...
  • 開源項目CRI-O(https://github.com/kubernetes-incubator/cri-o),即之前的OCID,旨在不依賴傳統容器引擎的前提下,使開源Kubernetes調度框架可以管理和啟動容器化的工作負載。 使用Google發起、Kubernetes工程師開發的容器運行時介面 ...
  • 在聚合的分組統計中我們會面臨兩種分組元素類型:連續型如時間,自然數等、離散型如地點、產品等。離散型數據本身就代表不同的組別,但連續型數據則需要手工按等長間隔進行切分了。下麵是一個按價錢段聚合的例子: POST /cartxns/_search { "size" : 1, "aggs": { "sal ...
  • mesos,節點,master,Mesos,marathon Mesos 安裝與使用 以 Mesos 結合 Marathon 應用框架為例,來看下如何快速搭建一套 Mesos 平臺。 Marathon 是可以跟 Mesos 一起協作的一個 framework,基於 Scala 實現,可以實現保持應用 ...
  • iptables,容器,0.0,Docker,訪問 容器訪問控制 容器的訪問控制,主要通過 Linux 上的 iptables 防火牆來進行管理和實現。iptables 是 Linux 上預設的防火牆軟體,在大部分發行版中都自帶。 容器訪問外部網路 容器要想訪問外部網路,需要本地系統的轉發支持。在L ...
  • 前言 本文的文字及圖片來源於網路,僅供學習、交流使用,不具有任何商業用途,版權歸原作者所有,如有問題請及時聯繫我們以作處理。 又到一年畢業季 時值畢業季,有不少小伙伴深受論文查重的困擾。因此我便想到做一個簡單的自動去重的工具,先看看效果,我們再對原理或是代碼實現做進一步的分析。 首先需要輸入appi ...
一周排行
    -Advertisement-
    Play Games
  • .Net8.0 Blazor Hybird 桌面端 (WPF/Winform) 實測可以完整運行在 win7sp1/win10/win11. 如果用其他工具打包,還可以運行在mac/linux下, 傳送門BlazorHybrid 發佈為無依賴包方式 安裝 WebView2Runtime 1.57 M ...
  • 目錄前言PostgreSql安裝測試額外Nuget安裝Person.cs模擬運行Navicate連postgresql解決方案Garnet為什麼要選擇Garnet而不是RedisRedis不再開源Windows版的Redis是由微軟維護的Windows Redis版本老舊,後續可能不再更新Garne ...
  • C#TMS系統代碼-聯表報表學習 領導被裁了之後很快就有人上任了,幾乎是無縫銜接,很難讓我不想到這早就決定好了。我的職責沒有任何變化。感受下來這個系統封裝程度很高,我只要會調用方法就行。這個系統交付之後不會有太多問題,更多應該是做小需求,有大的開發任務應該也是第二期的事,嗯?怎麼感覺我變成運維了?而 ...
  • 我在隨筆《EAV模型(實體-屬性-值)的設計和低代碼的處理方案(1)》中介紹了一些基本的EAV模型設計知識和基於Winform場景下低代碼(或者說無代碼)的一些實現思路,在本篇隨筆中,我們來分析一下這種針對通用業務,且只需定義就能構建業務模塊存儲和界面的解決方案,其中的數據查詢處理的操作。 ...
  • 對某個遠程伺服器啟用和設置NTP服務(Windows系統) 打開註冊表 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer 將 Enabled 的值設置為 1,這將啟用NTP伺服器功 ...
  • title: Django信號與擴展:深入理解與實踐 date: 2024/5/15 22:40:52 updated: 2024/5/15 22:40:52 categories: 後端開發 tags: Django 信號 松耦合 觀察者 擴展 安全 性能 第一部分:Django信號基礎 Djan ...
  • 使用xadmin2遇到的問題&解決 環境配置: 使用的模塊版本: 關聯的包 Django 3.2.15 mysqlclient 2.2.4 xadmin 2.0.1 django-crispy-forms >= 1.6.0 django-import-export >= 0.5.1 django-r ...
  • 今天我打算整點兒不一樣的內容,通過之前學習的TransformerMap和LazyMap鏈,想搞點不一樣的,所以我關註了另外一條鏈DefaultedMap鏈,主要調用鏈為: 調用鏈詳細描述: ObjectInputStream.readObject() DefaultedMap.readObject ...
  • 後端應用級開發者該如何擁抱 AI GC?就是在這樣的一個大的浪潮下,我們的傳統的應用級開發者。我們該如何選擇職業或者是如何去快速轉型,跟上這樣的一個行業的一個浪潮? 0 AI金字塔模型 越往上它的整個難度就是職業機會也好,或者說是整個的這個運作也好,它的難度會越大,然後越往下機會就會越多,所以這是一 ...
  • @Autowired是Spring框架提供的註解,@Resource是Java EE 5規範提供的註解。 @Autowired預設按照類型自動裝配,而@Resource預設按照名稱自動裝配。 @Autowired支持@Qualifier註解來指定裝配哪一個具有相同類型的bean,而@Resourc... ...