Exemplo n.º 1
0
        public void SaveMemo(string memoText)
        {
            var qry = from m in _db.Memo select m;

            if (qry.Count() > 0)
            {
                Memo m = qry.First();
                m.MemoText         = memoText;
                _db.Entry(m).State = System.Data.Entity.EntityState.Modified;
            }
            else
            {
                Memo m = new Memo();
                m.MemoText = memoText;
                _db.Memo.Add(m);
                _db.Entry(m).State = System.Data.Entity.EntityState.Added;
            }
            _db.SaveChanges();
        }
Exemplo n.º 2
0
        /// <summary>
        /// 更新股票代碼資訊
        /// </summary>
        /// <param name="stockDataList"></param>
        public void UpdateStockList(List <MyStockAnalyzer.Classes.StockData> stockDataList)
        {
            DateTime current = DateTime.Now;

            string[] stockIds = stockDataList.Select(x => x.StockId).ToArray();
            // 1. 先找出包含在資料庫的部分
            var existStockData = from s in _db.StockData
                                 where stockIds.Contains(s.StockId)
                                 select s;

            foreach (MyStockAnalyzer.Models.StockData stock in existStockData)
            {
                // 內容不一樣則更新
                MyStockAnalyzer.Classes.StockData compareStock = stockDataList.Where(x => x.StockId == stock.StockId).First();
                if (compareStock.StockName != stock.StockName || compareStock.Class != stock.Class || compareStock.Industry != stock.Industry || compareStock.WarrantTarget != stock.WarrantTarget)
                {
                    stock.StockName        = compareStock.StockName;
                    stock.Class            = compareStock.Class;
                    stock.Industry         = compareStock.Industry;
                    stock.WarrantTarget    = compareStock.WarrantTarget;
                    stock.Updated          = current;
                    _db.Entry(stock).State = System.Data.Entity.EntityState.Modified;
                }
            }

            // 2. 將不在資料庫的部分新增
            string[] existStockIds     = existStockData.Select(x => x.StockId).ToArray();
            var      nonExistStockData = from s in stockDataList where !existStockIds.Contains(s.StockId) select s;

            foreach (MyStockAnalyzer.Classes.StockData stock in nonExistStockData)
            {
                stock.Updated = current;
                _db.StockData.Add(new MyStockAnalyzer.Models.StockData()
                {
                    Class         = stock.Class,
                    Industry      = stock.Industry,
                    StockId       = stock.StockId,
                    StockName     = stock.StockName,
                    WarrantTarget = stock.WarrantTarget,
                    Updated       = stock.Updated
                });
            }

            _db.SaveChanges();
        }