选择具有LINQ查询的类的所有记录
本文关键字:记录 查询 LINQ 选择 | 更新日期: 2023-09-27 18:29:47
我正在尝试从linq查询中获取一些记录
dataentities db = new dataentities();
db.Tabel1.Select(x=> new lcclsAccDueList {LedgerName ,LedgerId,Amount ,AmountInString ,Expr2,Name ,StopID ,VIP }).ToList();
我做了这类
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; }
}
是否有其他方法可以选择所有记录,如
dataentities db = new dataentities();
db.Tabel1.Select(x=> new lcclsAccDueList { * }).ToList();
// * indicates select all records
您可以创建lcclsAccDueList
类构造函数,该构造函数接受source
并映射所有属性:
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; }
// (...)
}
然后在您的查询中使用它:
dataentities db = new dataentities();
db.Tabel1.Select(x => new lcclsAccDueList(x)).ToList();
或者您可以使用自动映射库,如automapper:
创建地图:
Mapper.CreateMap<tableItem, lcclsAccDueList>();
然后在查询中使用它:
dataentities db = new dataentities();
db.Tabel1.Select(x => Mapper.Map<lcclsAccDueList>(x);).ToList();