0

How do I get randomly similar data me tried to merge different ones in the first stage but Different function does not work,

    var turler= (from x in db.bird_table_ad
                join e in db.kus_resim on x.tr_x equals e.kus_tur
                where x.aile == item 

                select new GozlemTurleri
                {
                    id = x.id,
                    kod = x.kod,
                    tr_x = x.tr_x,
                    en_x = x.en_x,
                    lt_x = x.lt_x,

                    turfotourl="image_resize.phpad="+e.altDIR+"/"+e.resim+"&yon="+(e.galeri=="fg"?"2":"HD2"),
                    aile = x.aile,
                    gfoto = x.gfoto

                }).Distinct().ToList();

1 Answers1

1

If you try to get distinct record with regarding to tr_x from database then you can use GroupBy in entity framework.

So your code will be.

.GroupBy(x => x.tr_x).Select(x => x.First()).ToList();

Instead of

.Distinct().ToList();
er-sho
  • 9,581
  • 2
  • 13
  • 26
  • .GroupBy(x => x.tr_x).Select(x => x.First()).ToList(); did worked for me How can I make the selection randomly select(x=>x.random) ?? –  Nov 01 '18 at 07:05
  • could u plz explain why you need random selection? – er-sho Nov 01 '18 at 07:07
  • So i can get easily what you want :) – er-sho Nov 01 '18 at 07:42
  • You stated in your question that you need `randomly similar data` could u plz explain what this mean? – er-sho Nov 01 '18 at 09:57
  • tList = turler.GroupBy(f => new { b=f.tr_x,a=f.id } ).Select(f =>f.First()).ToList(); this code ; brings the first line after grouping I need you to query the column tList = turler.GroupBy(f => new { b=f.tr_x,a=f.id } ).Select(f =>f.Max(g=>g.mycolum)).ToList(); –  Nov 01 '18 at 11:12
  • so your query in above comment works or not? means `tList = turler.GroupBy(f => new { b=f.tr_x,a=f.id } ).Select(f =>f.Max(g=>g.mycolum)).ToList(); ` – er-sho Nov 01 '18 at 11:15
  • GroupBy(f => new { b=f.tr_x,a=f.id } ).Select(f =>f.First()).ToList(); this query works correctly but this code brings the group first element I need to get the most from the HITI column for element;;; .Select(f =>f.Max(g=>g.hiti)) –  Nov 01 '18 at 11:31
  • `I need to get the most from the HITI column` <= means you need to get those element that have max value of hiti column after grouping, right? – er-sho Nov 01 '18 at 11:41
  • Yes, not first- max HITI colum like this EXAMPLE SQL CODE SELECT id, MAX(rev) FROM YourTable GROUP BY id EF CODE ???? – –  Nov 01 '18 at 11:44
  • try this => `.GroupBy(f => new { b = f.tr_x, a = f.id }).Select(x => new { a = x.Key.a, b = x.Key.b, hiti = x.Max(y => y.hiti) }).ToList();` – er-sho Nov 01 '18 at 11:46
  • **OR** try this => `.GroupBy(f => new { b = f.tr_x, a = f.id }).Select(x => new { a = x.Key.a, b = x.Key.b, hiti = x.Select(y => y.hiti).Max() }).ToList();` – er-sho Nov 01 '18 at 11:55
  • If my answer was able to solve your problem then mark the tick on left side of answer to make it green, otherwise let me know if you faced any problem :) – er-sho Nov 03 '18 at 01:39