对象从存储库返回 [英] Object return from repository

查看:50
本文介绍了对象从存储库返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大家好,我真的需要帮助



我从存储库模式返回对象,对象是数据库实体(我无法更改)现在我需要发送返回价格总和(总数),它不是我要返回的对象的属性。我应该把结果送回去



如何更改此

Hi Guys, I really need help

I am returning object from repository pattern that object is database entity ( which i cannot change ) now I have requirement to send back sum of prices (total ) which is not an attribute of object I am going to return. what result should I send it back

how can i change this

List<Order>

以及其他内容它会对我有用



with something else so that it will do work for me

public List<Order> ListByCustomerId(int customerId)
		{


            var Sessionslist = (from OrderItems in DataContext.OrderItems
                                join odr in DataContext.Orders on new { ID = OrderItems.OrderId } equals new { ID = odr.ID } into odr_join
                                from odr in odr_join.DefaultIfEmpty()
                                join Products in DataContext.Products on new { ID = OrderItems.ProductId } equals new { ID = Products.ID } into Products_join
                                from Products in Products_join.DefaultIfEmpty()
                                where
                                  odr.CustomerId == 1
                                group new { odr, OrderItems, Products } by new
                                {
                                    odr.OrderNumber,
                                    odr.ID,
                                    odr.OrderDate
                                } into g
                                select new
                                {
                                    OrderNumber = g.Key.OrderNumber,
                                    ID = (int?)g.Key.ID,
                                    OrderDate = g.Key.OrderDate,
                                    total = (decimal?)g.Sum(p => p.Products.Price * p.OrderItems.Quantity)
                                }).ToList();




            List<Order> order = this.DataContext.Orders.Where(o => o.CustomerId == customerId).ToList();
            return order;





我的尝试:



我尝试了很多东西但没有任何工作



What I have tried:

I tried lot of thing but nothing worked

推荐答案

你需要为对象添加一个属性。尝试以下代码



You need to add an attribute to the object. try below code

public List<Order> ListByCustomerId(int customerId)
		{


            var Sessionslist = (from OrderItems in DataContext.OrderItems
                                join odr in DataContext.Orders on new { ID = OrderItems.OrderId } equals new { ID = odr.ID } into odr_join
                                from odr in odr_join.DefaultIfEmpty()
                                join Products in DataContext.Products on new { ID = OrderItems.ProductId } equals new { ID = Products.ID } into Products_join
                                from Products in Products_join.DefaultIfEmpty()
                                where
                                  odr.CustomerId == 1
                                group new { odr, OrderItems, Products } by new
                                {
                                    odr.OrderNumber,
                                    odr.ID,
                                    odr.OrderDate
                                } into g
                                select new Order
                                {
                                    OrderNumber = g.Key.OrderNumber,
                                    ID = (int?)g.Key.ID,
                                    OrderDate = g.Key.OrderDate,
                                    total = (decimal?)g.Sum(p => p.Products.Price * p.OrderItems.Quantity)
                                }).ToList();
  return Sessionslist 


这篇关于对象从存储库返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆