locked
How Can I get Distinct values? RRS feed

  • Question

  • Hi,

    I don't know  how to get distinct value using lambda linq.

    I can't find the option Distinct().

    Please help me.

    This the code

    public async Task<ObservableCollection<ClassJViewModel>> GetAllClassJsS()

            {

       classJs = new ObservableCollection<ClassJViewModel>();

                var db = new SQLite.SQLiteAsyncConnection(App.DBPath);

                var _classJs = await db.Table<ClassJ>().OrderBy(

                    p2 => p2.StartJ).ToListAsync();

                foreach (var _classJ in _classJs)

                {

        var classJ = new ClassJViewModel()

                    {

                         _id = _classJ._id,

                        SubjectNameJ = _classJ.SubjectNameJ,

                        SubjectColorJ = _classJ.SubjectColorJ,

                        TeacherNameJ = _classJ.TeacherNameJ,

                        TeacherNameShortJ = _classJ.TeacherNameShortJ,

                        RoomNameJ = _classJ.RoomNameJ,

                        YearJ = _classJ.YearJ,

                        MonthJ = _classJ.MonthJ,

                        MonthNameJ = _classJ.MonthNameJ,

                        DayJ = _classJ.DayJ,

                        ymdJ = _classJ.ymdJ,

                        ywdwStartJ = _classJ.ywdwStartJ,

                        ywdwStartSJ = _classJ.ywdwStartSJ,

                        ymJ = _classJ.ymJ,

                        WeekJ = _classJ.WeekJ,

                        DayOfWeekJ = _classJ.DayOfWeekJ,

                        DayOfWeekNameJ = _classJ.DayOfWeekNameJ,

                        ywdwJ = _classJ.ywdwJ,

                        ywJ = _classJ.ywJ,

                        StartJ = _classJ.StartJ,

                        StartMinJ = _classJ.StartMinJ,

                        StartAMPMJ = _classJ.StartAMPMJ,

                        EndJ = _classJ.EndJ,

                        EndMinJ = _classJ.EndMinJ,

                        EndAMPMJ = _classJ.EndAMPMJ,

                        DiffMinJ = _classJ.DiffMinJ,

                        PosXJ = _classJ.PosXJ,

                        PosYJ = _classJ.PosYJ,

                        HeightJ = _classJ.HeightJ,

                       DayOfWeekStartMinJ = _classJ.DayOfWeekStartMinJ,

                        HomeworkDescJ = _classJ.HomeworkDescJ,

                        EventDescJ = _classJ.EventDescJ,

                        NoteDescJ = _classJ.NoteDescJ,

                        HomeworkFixJ = _classJ.HomeworkFixJ,

                        EventFixJ = _classJ.EventFixJ,

                        NoteFixJ = _classJ.NoteFixJ

                    };

                    classJs.Add(classJ);

                                }

                return classJs;

            }


    ADRIAN DIBU

    Sunday, November 3, 2013 1:42 AM

Answers

  • Sunday, November 3, 2013 2:51 PM
    Moderator
  • Hi,

    Thanks for help.

    I found the solution:

     public class SubjColor
        {
            public string Subj { get; set; }
            public string Col { get; set; }
             public class Comparer : IEqualityComparer<SubjColor>
        {
            public bool Equals(SubjColor x, SubjColor y)
            {
                return x.Subj == y.Subj
                    && x.Col == y.Col;
            }

            public int GetHashCode(SubjColor obj)
            {
                return (obj.Subj + obj.Col).GetHashCode();
            }
        }
    }

    Usage:

    vardistinct = subjColors.Distinct(newSubjColor.Comparer());

    Thanks


    ADRIAN DIBU

    • Marked as answer by adibu Monday, November 4, 2013 2:00 PM
    Monday, November 4, 2013 2:00 PM

All replies

  • Hi Adrian,

    by what key do you want to destinct _classJs?


    lh

    Sunday, November 3, 2013 9:29 AM
  • Sunday, November 3, 2013 2:51 PM
    Moderator
  • Hi,

    Thanks for the prompt answer.

    The key is SubjectNameJ.

    Thanks

    Best regards


    ADRIAN DIBU

    Sunday, November 3, 2013 2:53 PM
  • Hi,

    Thanks Rob for the answer.

    I don't know how to use Enumerable.Distinct in my context.

    All what I need is to change something in the line

                var db = new SQLite.SQLiteAsyncConnection(App.DBPath);

                var _classJs = await db.Table<ClassJ>().OrderBy(

                    p2 => p2.StartJ).ToListAsync();

    Now it works OK I get multiple records and I want only one value

    Thanks


    ADRIAN DIBU

    Sunday, November 3, 2013 3:39 PM
  • Apply the Distinct method before the ToListAsync method:

    var _classJs = await db.Table<ClassJ>().OrderBy(p2 => p2.StartJ).Distinct().ToListAsync();
    

    Sunday, November 3, 2013 4:16 PM
  • Hi,

    Thanks Magnus for the prompt answer.

    I tried and I got

    'SQLite.AsyncTableQuery<Class_Time.Models.ClassJ>' does not contain a definition for 'Distinct' and no extension method 'Distinct' accepting a first argument of type 'SQLite.AsyncTableQuery<Class_Time.Models.ClassJ>' could be found (are you missing a using directive or an assembly reference?)

    Thanks


    ADRIAN DIBU

    Sunday, November 3, 2013 5:11 PM
  • Hi,

    I made 2 lists and I applied Distinct

    private async Task<string> GetDataSunSSS()

     {

          weekJsViewModel1 = new WeekJsViewModel();

          weekJs1 = new ObservableCollection<WeekJViewModel>();

          weekJs1 = await weekJsViewModel1.GetAllWeekJs();

         if (weekJs1.Count() > 0)

         {

             listSubjN = new List<string>();

             listSubjC = new List<string>();

             foreach (var prime995 in subjectJs1) // Loop through List with foreach

             {

                 listSubjN.Add(prime995.SubjectNameJ.ToString());

                 listSubjC.Add(prime995.SubjectColorJ.ToString());

             }

         }

         listSubjNDistinct =  listSubjN.Distinct().ToList();

         listSubjCDistinct = listSubjC.Distinct().ToList();

         SunClassJsViewSourceSSS.Source = listSubjCDistinct; // weekJs1;

         SunClassJsGridViewSSS.SelectedItem = null;

      return "Sunday";

     }

        The problem is I don't know how to Bind the two lists to the GridView

    When it was bind to database I used BindingSubjectNameJand BindingSubjectColorJ and it worked with weeekJs1.

    With lists what I suppose to use?????????

        <DataTemplate x:Key="ClassJTemplateSSS">

           <StackPanel  x:Name="panelMonSSS" Orientation="Horizontal" HorizontalAlignment="Left" Height="50" Background="{Binding SubjectColorJ}" Width="50">

     <TextBlock x:Name="txtSubjecNaameJSSS" Text="{Binding SubjectNameJ}" Foreground="Black" Height="19"  TextWrapping="Wrap" VerticalAlignment="Top" FontSize="14" FontWeight="SemiBold"/>

                    </StackPanel>

     </DataTemplate>

       <GridView x:Name="SunClassJsGridViewSSS" Background="Black"  Height="50"

                        Margin="0,0,0,0"

                        ScrollViewer.HorizontalScrollBarVisibility="Disabled"

                        Foreground="White"    

                        SelectionMode="Single"

                        IsItemClickEnabled="True"

                        ItemsSource="{Binding Mode=OneWay, Source={StaticResource SunClassJsViewSourceSSS}}"

                        ItemTemplate="{StaticResource ClassJTemplateSSS}"

                  >

         <GridView.ItemsPanel>

              <ItemsPanelTemplate>

                          <WrapGrid Orientation="Vertical"/>

              </ItemsPanelTemplate>

           </GridView.ItemsPanel>

            </GridView>

     Thanks

     


    ADRIAN DIBU

    Sunday, November 3, 2013 6:17 PM
  • Hi,

    Thanks Magnus for the prompt answer.

    I tried and I got

    'SQLite.AsyncTableQuery<Class_Time.Models.ClassJ>' does not contain a definition for 'Distinct' and no extension method 'Distinct' accepting a first argument of type 'SQLite.AsyncTableQuery<Class_Time.Models.ClassJ>' could be found (are you missing a using directive or an assembly reference?)

    Thanks


    ADRIAN DIBU


    You could apply the Distinct method on the returned list then:

    var db = new SQLite.SQLiteAsyncConnection(App.DBPath);
    List<ClassJ> _classJs = await db.Table<ClassJ>().OrderBy(p2 => p2.StartJ).ToListAsync();
    _classJs = _classJs.Distinct().ToList();
    

    Sunday, November 3, 2013 6:37 PM
  •  Hi,

    I tried

    public class SubjColor

        {

            public string Subj { get; set; }

            public string Col { get; set; }

        }

    private async Task<string> GetDataSunSS()

     {

          weekJsViewModel1 = new WeekJsViewModel();

          weekJs1 = new ObservableCollection<WeekJViewModel>();

          weekJs1 = await weekJsViewModel1.GetAllWeekJs();

         if (weekJs1.Count() > 0)

         {

             listSubjN = new List<string>();

             listSubjC = new List<string>();

             subjColors = new List<SubjColor>();

     

             foreach (var prime995 in weekJs1) // Loop through List with foreach

             {

                 listSubjN.Add(prime995.SubjectNameJ.ToString());

                 listSubjC.Add(prime995.SubjectColorJ.ToString());

                 subjColors.Add(new SubjColor { Subj = prime995.SubjectNameJ.ToString(), Col = prime995.SubjectColorJ.ToString() });

             }

                  listSubjNDistinct = new List<string>();

                  listSubjNDistinct =  listSubjN.Distinct().ToList();

                  listSubjCDistinct = new List<string>();

                  listSubjCDistinct = listSubjC.Distinct().ToList();

                  subjColorsDistinct = new List<SubjColor>();

                  subjColorsDistinct = subjColors.Distinct().ToList();

                 // SunClassJsViewSourceSS.Source = listSubjCDistinct; // works  colors are Distinct

                 // SunClassJsViewSourceSS.Source = listSubjNDistinct; // works names are distinct

                  // SunClassJsViewSourceSS.Source =  weekJs1; working but not distinct

                  SunClassJsViewSourceSS.Source = subjColorsDistinct; // works but not distinct ????????????? Why???

    weekJs1.Distinct().ToList();

    // SunClassJsViewSourceSS.Source =   // working but not distinct

                   SunClassJsGridViewSS.SelectedItem = null;

         }

         return "Sunday";

     }


    ADRIAN DIBU

    Sunday, November 3, 2013 11:59 PM
  • Hi,

    Thanks for help.

    I found the solution:

     public class SubjColor
        {
            public string Subj { get; set; }
            public string Col { get; set; }
             public class Comparer : IEqualityComparer<SubjColor>
        {
            public bool Equals(SubjColor x, SubjColor y)
            {
                return x.Subj == y.Subj
                    && x.Col == y.Col;
            }

            public int GetHashCode(SubjColor obj)
            {
                return (obj.Subj + obj.Col).GetHashCode();
            }
        }
    }

    Usage:

    vardistinct = subjColors.Distinct(newSubjColor.Comparer());

    Thanks


    ADRIAN DIBU

    • Marked as answer by adibu Monday, November 4, 2013 2:00 PM
    Monday, November 4, 2013 2:00 PM