0

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

如何访问 Helper Class 属性或如何获取 Classes 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

4

3 回答 3

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);
于 2013-05-10T14:21:28.890 回答
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;
}

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

于 2013-05-10T14:20:47.527 回答
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);
于 2013-05-10T14:22:41.193 回答