locked
How to make anonymous type linq query custom model binding RRS feed

  • Question

  • User-2088219670 posted

    Hi. I want bind my cutom model (DenetlenenViewModel) but "Yil" property is List. How to bind model linq query?

    My Custom Model

    public class DenetlenenViewModel
        {
            public int Id { get; set; }
            public string FirmaAdi { get; set; }
            public List<int> Yil { get; set; }
        }

    My  Method

    public IEnumerable<DenetlenenViewModel> YillarlaBirlikteGetir()
            {
                var firmalar = (from t in FASDBContext.Teklif
                                join d in FASDBContext.Denetlenen
                                on t.DenetlenenId.Value equals d.Id
                                select new DenetlenenViewModel
                                {
                                    Id = d.Id,
                                    FirmaAdi = d.FirmaAdi,
                                    Yil = List<t.Yil> // Error : 'List<t.Yil>' is a type, which is not valid in the given context 
                                }).ToList();
                return firmalar;
            }

    Wednesday, February 28, 2018 2:37 PM

Answers

  • User1168443798 posted

    Hi yclaydin,

    >> public int? Yil { get; set; } // "Yil" property int List

    To get Yil as “2014,2015,2016,2017”, you need to define the Yil as a string instead of int?.

    Here is a simple code:

    DenetlenenViewModel

        public class DenetlenenViewModel
        {
            public int Id { get; set; }
            public string FirmaAdi { get; set; }
            public string Yil { get; set; }       
        }

    Query

                var firmalar1 = (from t in _context.Teklif
                                join d in _context.Denetlenen
                                on t.DenetlenenId.Value equals d.Id
                                group t by d into g
                                select new DenetlenenViewModel
                                {
                                    Id = g.Key.Id,
                                    FirmaAdi = g.Key.FirmaAdi,
                                    Yil = string.Join(",", g.Select(i => i.Yil))
                                }).ToList();
    

    Best Regards,

    Edward

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 1, 2018 7:19 AM

All replies

  • User-474980206 posted

    its a List<int> so:

    public IEnumerable<DenetlenenViewModel> YillarlaBirlikteGetir()
            {
                var firmalar = (from t in FASDBContext.Teklif
                                join d in FASDBContext.Denetlenen
                                on t.DenetlenenId.Value equals d.Id
                                select new DenetlenenViewModel
                                {
                                    Id = d.Id,
                                    FirmaAdi = d.FirmaAdi,
                                    Yil = new List<int>(),
                                }).ToList();
                return firmalar;
            }

    this will of course make the list empty. probably not what you want. a join creates a product of the two tables, so you will get a duplicated row of the first table for each row in the second table. I assume you want the list of int to come from one of the tables. you will need to do a group by to do this.

    Wednesday, February 28, 2018 3:28 PM
  • User-2088219670 posted

    Hi, 

    Query :

      public IEnumerable<DenetlenenViewModel> YillarlaBirlikteGetir()
            {
                var firmalar = (from t in FASDBContext.Teklif
                                join d in FASDBContext.Denetlenen
                                on t.DenetlenenId.Value equals d.Id
                                select new DenetlenenViewModel
                                {
                                    Id = d.Id,
                                    FirmaAdi = d.FirmaAdi,
                                    Yil = t.Yil
                                }).Distinct().ToList();
                return firmalar;
    
            }

    Result :

    This Query Result

    I want to get the bottom result . How can I group to get the result. How to write a Linq query ?

    I Want This Result

    Custom View Model:

     public class DenetlenenViewModel
        {
            public int Id { get; set; }
            public string FirmaAdi { get; set; }
            public int? Yil { get; set; } // "Yil" property int List
        }

    Denetlenen Table Model : 

     public int Id { get; set; }
            public int DenetciId { get; set; }
            public string FirmaAdi { get; set; }
            public string Yetkili { get; set; }
            public string Tel { get; set; }
            public string Faks { get; set; }
            public string Adres { get; set; }
            public string Email { get; set; }
            public string WebAdresi { get; set; }
            public string TicaretSicilNo { get; set; }
            public string VergiDairesi { get; set; }
            public int? VergiNo { get; set; }
            public int? SektorId { get; set; }
            public string SektorAdi { get; set; }

    Teklif Table Model 

       public int Id { get; set; }
            public int? DenetlenenId { get; set; }
            public int? Yil { get; set; }
            public string DenetimTuru { get; set; }
            public string DenetimTuru2 { get; set; }
            public DateTime? BasTarihi { get; set; }
            public DateTime? BitisTarihi { get; set; }
            public bool? Kabul { get; set; }
            public double? TeklifUcreti { get; set; }
            public DateTime? TeklifTarihi { get; set; }
    
            public Denetlenen Denetlenen { get; set; }

    Thursday, March 1, 2018 6:42 AM
  • User1168443798 posted

    Hi yclaydin,

    >> public int? Yil { get; set; } // "Yil" property int List

    To get Yil as “2014,2015,2016,2017”, you need to define the Yil as a string instead of int?.

    Here is a simple code:

    DenetlenenViewModel

        public class DenetlenenViewModel
        {
            public int Id { get; set; }
            public string FirmaAdi { get; set; }
            public string Yil { get; set; }       
        }

    Query

                var firmalar1 = (from t in _context.Teklif
                                join d in _context.Denetlenen
                                on t.DenetlenenId.Value equals d.Id
                                group t by d into g
                                select new DenetlenenViewModel
                                {
                                    Id = g.Key.Id,
                                    FirmaAdi = g.Key.FirmaAdi,
                                    Yil = string.Join(",", g.Select(i => i.Yil))
                                }).ToList();
    

    Best Regards,

    Edward

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 1, 2018 7:19 AM