2 sskcal sskcal 于 2016.04.16 08:54 提问

MVC5 entity framework6 表链接查询 怎么回事?

model代码如下:

 public class Orders
    {
        [Key]
        public int OrdersId { set; get; }
        //订单号
        public string OrdersNum { set; get; }

        public string ProductName { set; get; }

        public int ProductId { set; get; }

        public string PriceName { set; get; }

        public int PriceId { set; get; }

        //订购时间
        public DateTime PlaceOrdersTime { set; get; }

        //付款时间
        public DateTime PaymentTime { set; get; }

        //零售价
        public decimal ProposalPrice { set; get; }

        //结算价
        public decimal SettlementPrice { set; get; }

        //成本价
        public decimal CostPrice { set; get; }

        //订购数量
        public int OrderQuantity { set; get; }

        //总价
        public decimal TotalPrice { set; get; }

        //支付方式
        public string PayMode { set; get; }

        //支付总金额
        public decimal PayTotalPrice { set; get; }

        //订单状态
        public string OrdersState { set; get; }
        //哪个用户下的订单
        public string UserId { set; get; }

        public virtual ICollection<OrdersInfo> OrdersInfo { get; set; }

    }


    public class OrdersInfo 
    {
        [Key]
        public int OrdersInfoId { set; get; }

        public int OrdersId { set; get; }

        //联系人
        public string Contacts { set; get; }
        public string PhoneNumber { set; get; }

        public string IdCardNumber { set; get; }

        //使用日期
        public DateTime UseDate { set; get; }

        //验证码
        public string Code { set; get; }

        //验证码数量
        public int CodeQuantity { set; get; }


        //验证码未使用数量
        public int CodeNotUsedQuantity { set; get; }

        //验证码使用数量
        public int CodeUseQuantity { set; get; }
        //验证码有效期
        public DateTime CodeShelfLife { set; get; }

        //验证码核销时间
        public DateTime CodeUsedTime { set; get; }

        public string Remark { set; get; }
        public virtual Orders Orders { get; set; }

    }

控制器代码如下:

         public ActionResult RestaurantOrdersList(string OrdersNum, string ProductName, string Contacts, string PhoneNumber, int page = 1)
        {
            ViewBag.OrdersNum = OrdersNum;
            ViewBag.ProductName = ProductName;
            ViewBag.Contacts = Contacts;
            ViewBag.PhoneNumber = PhoneNumber;

            int pageSize = 10;
            var OrdersDb = from a in db.Orders
                           join b in db.OrdersInfo
                           on new { a.OrdersId } equals new { b.OrdersId }
                           select new { a, b };


            var userId = User.Identity.GetUserId();

                OrdersDb.Where(s => s.a.UserId == userId);

                if (!string.IsNullOrEmpty(OrdersNum))
                {
                    OrdersDb = OrdersDb.Where(s => s.a.OrdersNum.IndexOf(OrdersNum) > -1);
                }
                if (!string.IsNullOrEmpty(ProductName))
                {
                    OrdersDb = OrdersDb.Where(s => s.a.ProductName.Contains(ProductName));
                }

                if (!string.IsNullOrEmpty(Contacts))
                {
                    OrdersDb = OrdersDb.Where(s => s.b.Contacts.Contains(Contacts));
                }
                if (!string.IsNullOrEmpty(PhoneNumber))
                {
                    OrdersDb = OrdersDb.Where(s => s.b.PhoneNumber == PhoneNumber);
                }

                ViewBag.RestaurantOrdersList = OrdersDb.OrderByDescending(x => x.a.PlaceOrdersTime).ToPagedList(page, pageSize);

            return View();
        }

View代码如下:

 @foreach (var l in list)
            {

                @l.a.ProductName
                        }

错误如下:

图片说明

4个回答

sskcal
sskcal   2016.04.16 09:13
已采纳

图片说明

caozhy 兄弟,还是不行。

caozhy
caozhy   Ds   Rxr 2016.04.16 09:05

@foreach (dynamic l in list)

cyangqing
cyangqing   2016.04.22 13:37

用导航属性,或者做个视图模型,另外,你return的视图里面没有给数据模型

wangzi_123456
wangzi_123456   2016.06.08 17:36

试图上using model所在命名空间

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!