sskcal
2016-04-16 00:54
采纳率: 40%
浏览 1.5k
已采纳

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 2016-04-16 01:13
    已采纳

    图片说明

    caozhy 兄弟,还是不行。

    点赞 打赏 评论
  • blownewbee 2016-04-16 01:05

    @foreach (dynamic l in list)

    点赞 打赏 评论
  • cyangqing 2016-04-22 05:37

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

    点赞 打赏 评论
  • wangzi_123456 2016-06-08 09:36

    试图上using model所在命名空间

    点赞 打赏 评论

相关推荐 更多相似问题