Click here to Skip to main content
15,886,422 members
Home / Discussions / C#
   

C#

 
QuestionInsert voice data down SQLServer Pin
Hoang Luc18-Nov-19 4:45
Hoang Luc18-Nov-19 4:45 
AnswerRe: Insert voice data down SQLServer Pin
OriginalGriff18-Nov-19 4:46
mveOriginalGriff18-Nov-19 4:46 
Questionadd code text to speech voices windows 7 ? Pin
Member 245846717-Nov-19 21:14
Member 245846717-Nov-19 21:14 
AnswerRe: add code text to speech voices windows 7 ? Pin
Pete O'Hanlon17-Nov-19 22:07
mvePete O'Hanlon17-Nov-19 22:07 
GeneralRe: add code text to speech voices windows 7 ? Pin
Member 245846720-Nov-19 15:45
Member 245846720-Nov-19 15:45 
GeneralRe: add code text to speech voices windows 7 ? Pin
Pete O'Hanlon20-Nov-19 21:47
mvePete O'Hanlon20-Nov-19 21:47 
GeneralRe: add code text to speech voices windows 7 ? Pin
Member 245846724-Nov-19 15:06
Member 245846724-Nov-19 15:06 
QuestionC# How to show my List of data in Pivot format Pin
Mou_kol16-Nov-19 8:17
Mou_kol16-Nov-19 8:17 
I have list of information with following fields:
Project    State         Title
  ABC      Resolved      Title1
  ABC      Pending       Title2
  DEF      Archived      Title3
  DEF      Resolved      Title4
  DEF      Committed     Title5
  DEF      Active        Title6

I want output in following format using c#

Project    Pending      Resolved       Committed     Active
  ABC         2            1              0            3
  DEF         1            3              1            15


C#
private void button2_Click(object sender, EventArgs e)
{
    List<Item> objItems = new List<Item>();

    objItems.Add(new Item(1, "ABC", "Title1", "Resolved"));
    objItems.Add(new Item(2, "ABC", "Title2", "Pending"));
    objItems.Add(new Item(3, "DEF", "Title3", "Archived"));
    objItems.Add(new Item(4, "DEF", "Title4", "Resolved"));
    objItems.Add(new Item(5, "DEF", "Title5", "Committed"));
    objItems.Add(new Item(6, "DEF", "Title6", "Active"));
    objItems.Add(new Item(7, "ABC", "Title2", "Pending"));

    var data = objItems.Pivot(c => c.Project, c => c.State, lst => lst.Sum(c=> c.State)).ToList();
    dataGridView1.DataSource = data;  
}

public class Item
{
    public int Id { get; set; }
    public string Project { get; set; }
    public string Title { get; set; }
    public string State { get; set; }

    public Item(int id, string project, string title, string state)
    {
        this.Id = id;
        this.Project = project;
        this.Title = title;
        this.State = state;
    }
}

    public static class Extension
    {

        public static DataTable ToPivotTable<T, TColumn, TRow, TData>(
            this IEnumerable<T> source,
            Func<T, TColumn> columnSelector,
            Expression<Func<T, TRow>> rowSelector,
            Func<IEnumerable<T>, TData> dataSelector)
        {
            DataTable table = new DataTable();
            var rowName = ((MemberExpression)rowSelector.Body).Member.Name;
            table.Columns.Add(new DataColumn(rowName));
            var columns = source.Select(columnSelector).Distinct();

            foreach (var column in columns)
                table.Columns.Add(new DataColumn(column.ToString()));

            var rows = source.GroupBy(rowSelector.Compile())
                             .Select(rowGroup => new
                             {
                                 Key = rowGroup.Key,
                                 Values = columns.GroupJoin(
                                     rowGroup,
                                     c => c,
                                     r => columnSelector(r),
                                     (c, columnGroup) => dataSelector(columnGroup))
                             });

            foreach (var row in rows)
            {
                var dataRow = table.NewRow();
                var items = row.Values.Cast<object>().ToList();
                items.Insert(0, row.Key);
                dataRow.ItemArray = items.ToArray();
                table.Rows.Add(dataRow);
            }

            return table;
        }

    public static Dictionary<TKey1, Dictionary<TKey2, TValue>> Pivot<TSource, TKey1, TKey2, TValue>(
    this IEnumerable<TSource> source
    , Func<TSource, TKey1> key1Selector
    , Func<TSource, TKey2> key2Selector
    , Func<IEnumerable<TSource>, TValue> aggregate)
        {
            return source.GroupBy(key1Selector).Select(
                x => new
                {
                    X = x.Key,
                    Y = source.GroupBy(key2Selector).Select(
                        z => new
                        {
                            Z = z.Key,
                            V = aggregate(from item in source
                                          where key1Selector(item).Equals(x.Key)
                                          && key2Selector(item).Equals(z.Key)
                                          select item
                            )

                        }
                    ).ToDictionary(e => e.Z, o => o.V)
                }
            ).ToDictionary(e => e.X, o => o.Y);
        } 
    }


This Pivot code i got from here https://stackoverflow.com/a/6282079

Tell me how to arrange my data using above Pivot function. please tell me what to rectify in code to get desire output. thanks
QuestionRe: C# How to show my List of data in Pivot format Pin
Eddy Vluggen16-Nov-19 9:30
professionalEddy Vluggen16-Nov-19 9:30 
AnswerRe: C# How to show my List of data in Pivot format Pin
Richard Deeming17-Nov-19 22:57
mveRichard Deeming17-Nov-19 22:57 
Questionprint statements within a method is not displaying on Rich text box in c# windows form Pin
Member 1465244915-Nov-19 18:35
Member 1465244915-Nov-19 18:35 
AnswerRe: print statements within a method is not displaying on Rich text box in c# windows form Pin
OriginalGriff15-Nov-19 19:57
mveOriginalGriff15-Nov-19 19:57 
GeneralRe: print statements within a method is not displaying on Rich text box in c# windows form Pin
Member 1465244915-Nov-19 22:30
Member 1465244915-Nov-19 22:30 
GeneralRe: print statements within a method is not displaying on Rich text box in c# windows form Pin
OriginalGriff15-Nov-19 22:48
mveOriginalGriff15-Nov-19 22:48 
QuestionGetValueOrDefault() Throws Pin
Kevin Marois15-Nov-19 6:35
professionalKevin Marois15-Nov-19 6:35 
AnswerRe: GetValueOrDefault() Throws Pin
Eddy Vluggen15-Nov-19 6:50
professionalEddy Vluggen15-Nov-19 6:50 
GeneralRe: GetValueOrDefault() Throws Pin
Kevin Marois15-Nov-19 8:09
professionalKevin Marois15-Nov-19 8:09 
GeneralRe: GetValueOrDefault() Throws Pin
Eddy Vluggen15-Nov-19 9:16
professionalEddy Vluggen15-Nov-19 9:16 
GeneralRe: GetValueOrDefault() Throws Pin
Kevin Marois15-Nov-19 9:23
professionalKevin Marois15-Nov-19 9:23 
GeneralRe: GetValueOrDefault() Throws Pin
Eddy Vluggen16-Nov-19 0:31
professionalEddy Vluggen16-Nov-19 0:31 
QuestionRe: GetValueOrDefault() Throws Pin
Eddy Vluggen17-Nov-19 0:34
professionalEddy Vluggen17-Nov-19 0:34 
AnswerRe: GetValueOrDefault() Throws Pin
Richard Deeming17-Nov-19 22:53
mveRichard Deeming17-Nov-19 22:53 
GeneralRe: GetValueOrDefault() Throws Pin
Kevin Marois18-Nov-19 8:40
professionalKevin Marois18-Nov-19 8:40 
GeneralRe: GetValueOrDefault() Throws Pin
Richard Deeming18-Nov-19 8:48
mveRichard Deeming18-Nov-19 8:48 
GeneralRe: GetValueOrDefault() Throws Pin
Kevin Marois18-Nov-19 8:54
professionalKevin Marois18-Nov-19 8:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.