سؤال

I am trying get some records from a linq query

dataentities db = new dataentities();
db.Tabel1.Select(x=> new lcclsAccDueList {LedgerName ,LedgerId,Amount ,AmountInString ,Expr2,Name ,StopID ,VIP  }).ToList();

I made this class

 public class lcclsAccDueList
    {
        public string LedgerName { get; set; }
        public decimal LedgerId { get; set; }
        public Nullable<decimal> Amount { get; set; }
        public string AmountInString { get; set; }
        public Nullable<int> Expr2 { get; set; }
        public string Name { get; set; }
        public Nullable<decimal> StopID { get; set; }
        public Nullable<bool> VIP { get; set; }
    }

Is there any other way to select all records like

dataentities db = new dataentities();
db.Tabel1.Select(x=> new lcclsAccDueList { * }).ToList();
// * indicates select all records 
هل كانت مفيدة؟

المحلول

You can create lcclsAccDueList class constructor which takes source and maps all the properties:

public class lcclsAccDueList
{
    public lcclsAccDueList() {}
    public lcclsAccDueList(tableItem source)
    {
        LedgerName = source.LedgerName;
        LedgerId = source.LedgerId;
        // (...)
    }

    public string LedgerName { get; set; }
    public decimal LedgerId { get; set; }
    // (...)
}

And then use it within your query:

dataentities db = new dataentities();
db.Tabel1.Select(x => new lcclsAccDueList(x)).ToList();

Or you can use auto mapping libraries, like automapper:

Create map:

Mapper.CreateMap<tableItem, lcclsAccDueList>();

And then use it within you query:

dataentities db = new dataentities();
db.Tabel1.Select(x => Mapper.Map<lcclsAccDueList>(x);).ToList();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top