如何向List< Products>添加一列? ? [英] How can I add one more column to List<Products> ?
本文介绍了如何向List< Products>添加一列? ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在db中有一个数据表。
I have a data table in db.
ID || ProductName || SoldDateTime
==================================
1 || Books || 6543213571
2 || Pencils || 1594563215
3 || Paint || 5411865446
4 || Markers || 2421375125
5 || Copies || 5546423010
. || . || 7568714312
我在项目中添加了它作为ProdutEntityModel
现在我想将SoldDateTime转换为01/24/2015,结果我想要List< product>我怎样才能实现这个目标。
I added it in my project as ProdutEntityModel
Now i want to convert SoldDateTime into 01/24/2015 and the resultant i want List<product> how can i achieve this.
ID || ProductName || SoldDateTime || SDateTime
==========================================================
1 || Books || 6543213571 || 01/01/2015 06:22:15
2 || Pencils || 1594563215 || 01/08/2015 15:25:55
3 || Paint || 5411865446 || 01/15/2015 23:45:32
4 || Markers || 2421375125 || 01/22/2015 00:56:33
5 || Copies || 5546423010 || 02/12/2015 18:04:50
. || . || . || .
如何在Linq to Entity中执行此操作。
How can i do this in Linq to Entity.
推荐答案
这是这样的: http://robbincremers.me/2012/01/31/entity-framework-using-partial-classes-to-add-business-logic-and-validation-to-generated-entities/ 一> [ ^ ]
根据我的理解,您可以通过两种方式实现:
1。添加部分类
通过 Zoltan Zorgo [ ^ ]
或
2。将未映射的字段添加到您的模型
As per my understanding, you can achieve that using 2 ways:
1. add partial class
See solution 1 by Zoltan Zorgo[^]
or
2. add not mapped field to your model
[NotMapped]
public DateTime SDateTime {get; set;}
但你标记了你的问题: Linq ,所以这可能意味着你想根据你的模型创建查询...
But you tag your question: Linq, so it might means you want to create query based on your model...
var qry = from emd in EntityModelData
select new {
//get collection of your fields
ID = ID,
ProductName = ProductName,
SoldDateTime = SoldDateTime,
SDateTime = YourExtensionMethodToConvert(SoldDateTime)
};
第一步:只选择所需数据。
Step1: Select only the required data.
List<products> prodlist = ctx.Products
.Where(
x => SqlFunctions.DateAdd("s", x.SoldDateTime , "1970-01-01").Value.Year==DateTime.Now.Year &&
SqlFunctions.DateAdd("s", x.SoldDateTime , "1970-01-01").Value.Month==DateTime.Now.Month ).ToList();
</products>
Step2:我添加了Partial Class而不是Mapped Property
Step2: I added Partial Class and not Mapped Property
namespace MyShop
{
public partial class Products
{
private DateTime s_date = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
public DateTime SDateTime
{
get
{
return s_date.AddSeconds(Convert.ToDouble(this.SoldDateTime)).ToLocalTime();
}
}
}
}
我要感谢Zoltan Zorgo获取此帮助。
这篇关于如何向List< Products>添加一列? ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文