Click here to Skip to main content
14,390,032 members
   

C#

 
QuestionInsert voice data down SQLServer Pin
Hoang Luc18-Nov-19 5:45
MemberHoang Luc18-Nov-19 5:45 
AnswerRe: Insert voice data down SQLServer Pin
OriginalGriff18-Nov-19 5:46
mveOriginalGriff18-Nov-19 5:46 
Questionadd code text to speech voices windows 7 ? Pin
Member 245846717-Nov-19 22:14
MemberMember 245846717-Nov-19 22:14 
AnswerRe: add code text to speech voices windows 7 ? Pin
Pete O'Hanlon17-Nov-19 23:07
mvePete O'Hanlon17-Nov-19 23:07 
GeneralRe: add code text to speech voices windows 7 ? Pin
Member 245846720-Nov-19 16:45
MemberMember 245846720-Nov-19 16:45 
GeneralRe: add code text to speech voices windows 7 ? Pin
Pete O'Hanlon20-Nov-19 22:47
mvePete O'Hanlon20-Nov-19 22:47 
GeneralRe: add code text to speech voices windows 7 ? Pin
Member 245846724-Nov-19 16:06
MemberMember 245846724-Nov-19 16:06 
QuestionC# How to show my List of data in Pivot format Pin
Mou_kol16-Nov-19 9:17
MemberMou_kol16-Nov-19 9: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


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 10:30
mveEddy Vluggen16-Nov-19 10:30 
AnswerRe: C# How to show my List of data in Pivot format Pin
Richard Deeming17-Nov-19 23:57
mveRichard Deeming17-Nov-19 23:57 
Questionprint statements within a method is not displaying on Rich text box in c# windows form Pin
Member 1465244915-Nov-19 19:35
MemberMember 1465244915-Nov-19 19:35 
AnswerRe: print statements within a method is not displaying on Rich text box in c# windows form Pin
OriginalGriff15-Nov-19 20:57
mveOriginalGriff15-Nov-19 20:57 
GeneralRe: print statements within a method is not displaying on Rich text box in c# windows form Pin
Member 1465244915-Nov-19 23:30
MemberMember 1465244915-Nov-19 23:30 
GeneralRe: print statements within a method is not displaying on Rich text box in c# windows form Pin
OriginalGriff15-Nov-19 23:48
mveOriginalGriff15-Nov-19 23:48 
QuestionGetValueOrDefault() Throws Pin
Kevin Marois15-Nov-19 7:35
professionalKevin Marois15-Nov-19 7:35 
AnswerRe: GetValueOrDefault() Throws Pin
Eddy Vluggen15-Nov-19 7:50
mveEddy Vluggen15-Nov-19 7:50 
GeneralRe: GetValueOrDefault() Throws Pin
Kevin Marois15-Nov-19 9:09
professionalKevin Marois15-Nov-19 9:09 
GeneralRe: GetValueOrDefault() Throws Pin
Eddy Vluggen15-Nov-19 10:16
mveEddy Vluggen15-Nov-19 10:16 
GeneralRe: GetValueOrDefault() Throws Pin
Kevin Marois15-Nov-19 10:23
professionalKevin Marois15-Nov-19 10:23 
GeneralRe: GetValueOrDefault() Throws Pin
Eddy Vluggen16-Nov-19 1:31
mveEddy Vluggen16-Nov-19 1:31 
QuestionRe: GetValueOrDefault() Throws Pin
Eddy Vluggen17-Nov-19 1:34
mveEddy Vluggen17-Nov-19 1:34 
AnswerRe: GetValueOrDefault() Throws Pin
Richard Deeming17-Nov-19 23:53
mveRichard Deeming17-Nov-19 23:53 
GeneralRe: GetValueOrDefault() Throws Pin
Kevin Marois18-Nov-19 9:40
professionalKevin Marois18-Nov-19 9:40 
GeneralRe: GetValueOrDefault() Throws Pin
Richard Deeming18-Nov-19 9:48
mveRichard Deeming18-Nov-19 9:48 
GeneralRe: GetValueOrDefault() Throws Pin
Kevin Marois18-Nov-19 9:54
professionalKevin Marois18-Nov-19 9: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.