如何使用Linq查询C#从helper类属性中检索数据

时间:2013-05-10 14:01:10

标签: c# linq

我想从CPayment检索数据列表,但是当我编写linq查询时,会加载CPaymentModel类属性,但TransactionHelper类属性未加载且无法查看

如何访问Helper类属性或如何获取类Helper和Non-Helper属性的列表?如何解决此问题..

我的模型类代码

public class CPaymentModel<br>
{   
     public CPaymentModel()<br>
     {
        Transaction =new TransactionHelper();
     }

      //Local Class Properties     
      public UInt64 CardNo { get; set; }
      public DateTime FileDate { get; set; }        

      //Helper Class 
      public TransactionHelper Transaction { get; set; }
}

public class TransactionHelper<br>
{
      public int TransCode { get; set; }
      public DateTime TransDate { get; set; } 
}

控制器代码为:

public ActionResult GetPersons([DataSourceRequest] DataSourceRequest dsRequest)
{
  var session = SessionManager.GetCurrentSession();<br>
  var data2 = session.Query < CPayment > ().Select(row => new CPaymentModel()
  {
        CardNo = row.CardNo,<br>
        FileDate = row.FileDate,<br>
        Transaction =row.Transaction<br>
   }).ToList < CPaymentModel > ();<br>
  var list = data2.ToDataSourceResult(dsRequest);
  return Json(list);
}

索引页面(KendoUI-grid)代码:

@(Html.Kendo().Grid < CPaymentModel > ()
    .Name("PaymentInfo")
    .DataSource(dataSource => dataSource
    .Ajax()<br>
    .Read(read => read.Action("GetPersons", "KendoDemo"))
 )
)

我的输出是: [点击] http://i.stack.imgur.com/ZjKPU.png

3 个答案:

答案 0 :(得分:1)

试试这个

var session = SessionManager.GetCurrentSession();

var data2 = session.Query<CPayment>().Select(row => new CPaymentModel()
{
    CardNo = row.CardNo,
    Product = row.ProductDetails.Product,
    Subproduct = row.ProductDetails.Subproduct,
    FileDate = row.FileDate,
    Transaction =new TransactionHelper()
    {
        TransCode = row.Transaction.TransCode;
        TransDate = row.Transaction.TransDate
    }
}).ToList<CPaymentModel>();
var list = data2.ToDataSourceResult(dsRequest);

答案 1 :(得分:0)

如果要创建事务助手的新对象,请使用此方法。这里我创建了一个新对象,并使用初始化器语法来设置属性。

var session = SessionManager.GetCurrentSession();

var data2 = session.Query<CPayment>().Select(row => new CPaymentModel()
{
    CardNo = row.CardNo,
    Product = row.ProductDetails.Product,
    Subproduct = row.ProductDetails.Subproduct,
    FileDate = row.FileDate,
    Transaction = new TransactionHelper
    {
        TransCode = row.Transaction.TransCode,
        TransDate = row.Transaction.TransDate
    }
}).ToList<CPaymentModel>();
var list = data2.ToDataSourceResult(dsRequest);

您知道吗?您可以随时编写 lambda函数而不是 lambda表达式,如:

row =>
{
    CPaymentModel cpModel = new CPaymentModel()
    {
        CardNo = row.CardNo,
        Product = row.ProductDetails.Product,
        Subproduct = row.ProductDetails.Subproduct,
        FileDate = row.FileDate,
        Transaction = new TransactionHelper()
    };
    cpModel.Transaction = new Transaction();
    cpModel.Transaction.TransCode = row.Transaction.TransCode;
    cpModel.Transaction.TransDate = row.Transaction.TransDate;

    return cpModel;
}

这是一个完全有效的结构。当你不能用初始化器做任何事情时,它会很有用。

答案 2 :(得分:0)

也许调用带有Transaction属性路径的Include方法作为参数,表明EF应该用所有属性加载它

var session = SessionManager.GetCurrentSession();

 var data2 = session.Query<CPayment>().**Include("CPayment.Transaction").**Select(row => new CPaymentModel()
 {
        CardNo = row.CardNo,
        Product = row.ProductDetails.Product,
        Subproduct = row.ProductDetails.Subproduct,
        FileDate = row.FileDate,
        Transaction =row.Transaction
   }).ToList<CPaymentModel>();
var list = data2.ToDataSourceResult(dsRequest);