示例#1
0
        /// <summary>
        ///     Adds the Thanks info to a dataTable
        /// </summary>
        /// <param name="dataRows"> The data Rows. </param>
        public void AddThanksInfo(IEnumerable <DataRow> dataRows)
        {
            var messageIds = dataRows.Select(x => x.Field <int>("MessageID"));

            // Initialize the "IsthankedByUser" column.
            dataRows.ForEach(x => x["IsThankedByUser"] = false);

            // Initialize the "Thank Info" column.
            dataRows.ForEach(x => x["ThanksInfo"] = string.Empty);

            // Iterate through all the thanks relating to this topic and make appropriate
            // changes in columns.
            var allThanks = LegacyDb.MessageGetAllThanks(messageIds.ToDelimitedString(",")).ToList();

            foreach (var f in
                     allThanks.Where(t => t.FromUserID != null && t.FromUserID == YafContext.Current.PageUserID).SelectMany(
                         thanks => dataRows.Where(x => x.Field <int>("MessageID") == thanks.MessageID)))
            {
                f["IsThankedByUser"] = "******";
                f.AcceptChanges();
            }

            var thanksFieldNames = new[] { "ThanksFromUserNumber", "ThanksToUserNumber", "ThanksToUserPostsNumber" };

            foreach (DataRow postRow in dataRows)
            {
                var messageId = postRow.Field <int>("MessageID");

                postRow["MessageThanksNumber"] =
                    allThanks.Count(t => t.FromUserID != null && t.MessageID == messageId);

                var thanksFiltered = allThanks.Where(t => t.MessageID == messageId);

                if (thanksFiltered.Any())
                {
                    var thanksItem = thanksFiltered.First();

                    postRow["ThanksFromUserNumber"]    = thanksItem.ThanksFromUserNumber ?? 0;
                    postRow["ThanksToUserNumber"]      = thanksItem.ThanksToUserNumber ?? 0;
                    postRow["ThanksToUserPostsNumber"] = thanksItem.ThanksToUserPostsNumber ?? 0;
                }
                else
                {
                    DataRow row = postRow;
                    thanksFieldNames.ForEach(f => row[f] = 0);
                }

                // load all all thanks info into a special column...
                postRow["ThanksInfo"] = thanksFiltered
                                        .Where(t => t.FromUserID != null)
                                        .Select(x => "{0}|{1}".FormatWith(x.FromUserID.Value, x.ThanksDate))
                                        .ToDelimitedString(",");

                postRow.AcceptChanges();
            }
        }