Example #1
0
        public SalesTotalResponse Split(SalesTotalRequest request)
        {
            var    response    = new SalesTotalResponse();
            string path1       = @"E:\kanjiCheck.txt";
            string newFilePath = @"E:\ccc\" + "file3" + ".txt";

            string[] lines = File.ReadAllLines(path1);

            var writes   = new List <string>();
            var fileName = "";
            var index    = 0;

            foreach (var line in lines.Where(e => e != "" && e != "    "))
            {
                if (index == 0)
                {
                    fileName = line.Split('"')[1].Replace("|", "_").Replace("/", "_").Replace("!", "_").Replace("?", "_");
                }
                if (!line.Contains("---"))
                {
                    writes.Add(line);
                    index++;
                }
                else
                {
                    newFilePath = @"C:\ccc\" + fileName + @".txt";
                    File.WriteAllLines(newFilePath, writes);
                    writes = new List <string>();
                    index  = 0;
                }
            }
            return(response);
        }
Example #2
0
        public SalesTotalResponse SalesTotal(SalesTotalRequest request)
        {
            var               response         = new SalesTotalResponse();
            MongoClient       client           = new MongoClient("mongodb://localhost:27017");
            MongoDatabaseBase db               = client.GetDatabase("ntut") as MongoDatabaseBase;
            var               memberCollection = db.GetCollection <SalesCollection>("sales");

            #region
            var query = Builders <SalesCollection> .Filter.And(
                Builders <SalesCollection> .Filter.Gte(e => e.date, request.dateLower),
                Builders <SalesCollection> .Filter.Lte(e => e.date, request.dateUpper));

            #endregion
            var result = memberCollection.Aggregate().Match(e => e.date >= request.dateLower && e.date <= request.dateUpper)
                         .Group(e => new SalesTotalDate
            {
                year  = e.date.Year,
                month = e.date.Month,
                day   = e.date.Day
            },
                                e => new SalesTotalItem
            {
                _id             = e.Key,
                totalSaleAmount = e.Sum(p => p.price * p.quantity),
                averageQuantity = e.Average(p => p.quantity),
                count           = e.Count()
            })
                         .ToList();
            #region 方法2 使用BsonDocument,但field看不到參考
            var groupBson2 = new BsonDocument
            {
                {
                    "_id", new BsonDocument
                    {
                        { "month", new BsonDocument("$month", "$date") },
                        { "day", new BsonDocument("$dayOfMonth", "$date") },
                        { "year", new BsonDocument("$year", "$date") }
                    }
                },
                {
                    "totalSaleAmount", new BsonDocument("$sum", new BsonDocument("$multiply", new BsonArray {
                        "$price", "$quantity"
                    }))
                },
                {
                    "averageQuantity", new BsonDocument("$avg", "$quantity")
                },
                {
                    "count", new BsonDocument("$sum", 1)
                }
            };
            var result2 = memberCollection.Aggregate().Match(query)
                          .Group <SalesTotalItem>(groupBson2)
                          .ToList();
            #endregion
            #region 方法3 使用dateToString,但輸出不反序列化,因為原本的_id是物件,而此處是字串
            var groupBson3 = new BsonDocument
            {
                {
                    "_id", new BsonDocument("$dateToString", new BsonDocument {
                        { "format", "%Y-%m-%d" }, { "date", "$date" }
                    })
                },
                {
                    "totalSaleAmount", new BsonDocument("$sum", new BsonDocument("$multiply", new BsonArray {
                        "$price", "$quantity"
                    }))
                },
                {
                    "averageQuantity", new BsonDocument("$avg", "$quantity")
                },
                {
                    "count", new BsonDocument("$sum", 1)
                }
            };
            var result3 = memberCollection.Aggregate().Match(query)
                          .Group(groupBson3)
                          .ToList();
            #endregion
            #region 方法4 弱型別
            var result4 = memberCollection.Aggregate().Match(query)
                          .Group("{_id:{$dateToString:{format:\"%Y-%m-%d\",date:\"$date\"}},totalSaleAmount:{$sum:{$multiply:[\"$price\",\"$quantity\"]}},averageQuantity:{$avg:\"$quantity\"},count:{$sum:1}}")
                          .ToList();
            #endregion

            response.items = result;
            return(response);
        }