简体   繁体   中英

linq selecting problem (distinct)

        List<NastavaIzvjestaj> nastava_izvjestaj = new List<NastavaIzvjestaj>();
        var data_context = new DataEvidencijaDataContext();
        int pomSum = 0;

        var prisustvo = (from j in data_context.nastava_prisustvos 
                select j.br_indexa).Distinct();
        var lista = prisustvo.ToList();

        foreach (var i in prisustvo)
        {
            var pom = from k in data_context.nastava_prisustvos

                      where k.br_indexa == i
                      select k.broj_casova;

            pomSum = pom.Sum();

            nastava_izvjestaj.Add(new NastavaIzvjestaj
            {
                br_indexa = i.br_indexa,
                naziv = "mjau",
                ime = "First Name",
                prezime = "Last Name",
                tip_nastave = "XXX",
                procenat_prisustva =pomSum
            });
        }

            string reportPath = Server.MapPath("NastavaPrikaz.rpt");

            ObjektniNastavaIzvjestaj = new ReportDocument();
            ObjektniNastavaIzvjestaj.Load(reportPath);
            ObjektniNastavaIzvjestaj.SetDataSource(nastava_izvjestaj);
            CrystalReportViewer1.ReportSource = ObjektniNastavaIzvjestaj;

This code return something like this:

           Br_idexa        Ime          Prezime         Procenat

           185             First Name   Last Name           30
           185             First Name   Last Name           30
           185             First Name   Last Name           30
           185             First Name   Last Name           30

How do I get only one row :

           185             First Name   Last Name           30

problem is solved

In your query you can select which column you want to select distinct on:

var prisustvo = (from j in data_context.nastava_prisustvos 
                    select j.br_indexa).Distinct();

Try adding a partial class for your NastavaIzvjestaj that does:

namespace ...
public partial class NastavaIzvjestaj
{
   public override bool Equals(object o)
   {
      if (o == null || !(o is NastavaIzvjestaj))
         return false;

      return this.Br_idexa == o.Br_idexa;
   }

   public override int GetHashCode()
   {
      return this.Br_idexa.GetHashCode();
   }
}

Providing that Br_idexa is your unique identifier and pk.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM