2016-04-11 110 views
2

我對C#很陌生,仍然在學習,如果這是一個簡單的問題,我很抱歉。 當用戶訂購一個或多個項目時,將爲這些項目分配一個OrderId,然後使用此OrderId將其與訂單相關聯。現在,但是我需要鏈接這兩個並顯示它們,因此只有訂購它們的用戶才能查看它們(基於存儲在訂單類中的用戶名)C#訂單清單

我會如何在ViewOrdersController中執行此操作?

當前職業: 訂購

[Bind(Exclude = "OrderId")] 
    public partial class Order 
    { 
     [ScaffoldColumn(false)] 
     public int OrderId { get; set; } 

     [ScaffoldColumn(false)] 
     public System.DateTime OrderDate { get; set; } 

     [ScaffoldColumn(false)] 
     [Remote("CheckUserName", "Account")] 
     public string Username { get; set; } 

     [Required] 
     [StringLength(16, ErrorMessage = "Your name is too long")] 
     [Display(Name = "First Name")] 
     public string FirstName { get; set; } 

     [Required(ErrorMessage = "Your last name is required.")] 
     [StringLength(16, ErrorMessage = "Last name is too long.")] 
     [Display(Name = "Last Name")] 
     public string LastName { get; set; } 

     [Required(ErrorMessage = "Address is required.")] 
     public string Address { get; set; } 

     [Required(ErrorMessage = "City is required.")] 
     public string City { get; set; } 

     [Required(ErrorMessage = "Postcode is required.")] 
     [Display(Name = "Post Code")] 
     public string PostalCode { get; set; } 

     [Required(ErrorMessage = "Country is required.")] 
     public string Country { get; set; } 

     [Required(ErrorMessage = "Phone number is required.")] 
     public string Phone { get; set; } 

     [RegularExpression(@"[A-Za-z0-9._%+-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}", ErrorMessage = "Email doesn't look like a valid email address.")] 
     public string Email { get; set; } 

     [System.ComponentModel.DataAnnotations.Compare("Email")] 
     [Display(Name = "Confirm your email address")] 
     public string EmailConfirm { get; set; } 


     [ScaffoldColumn(false)] 
     public string PaymentTransactionId { get; set; } 


     public bool HasBeenShipped { get; set; } 

     [ScaffoldColumn(false)] 
     [ReadOnly(true)] 
     public decimal Total { get; set; } 

     public List<OrderDetail> OrderDetails { get; set; } 
    } 

的OrderDetail

public class OrderDetail 
    { 
     public int OrderDetailId { get; set; } 
     public int OrderId { get; set; } 
     public int ProductId { get; set; } 
     public int Quantity { get; set; } 
     public decimal UnitPrice { get; set; } 
     public virtual Product Product { get; set; } 
     public virtual Order Order { get; set; } 
    } 

我試着去創建一個控制器,允許對已作出的命令來查看自己的訂單的用戶

+2

你的問題完全含糊 - 你可以顯示你當前的課程嗎? 另外,這是什麼ViewOrdersController?列表顯示項目?定製控制器? – ArielB

+0

顯示你已經嘗試過的一些代碼? –

+2

對不起回覆我意識到它是多麼模糊,在一瞬間編輯不了主要的帖子 –

回答

1
int orderId = // The orderId you want to use 
Items.Where(item => item.OrderId == orderId) 

這個例子是用Linq(.Where函數)完成的d與lambda表達樹(=>)。希望這可以幫助。我建議讀入linq,因爲它與.Net/C#相比,Java /其他語言的功能如果不是最強大的方面之一。

+0

謝謝,我現在對此感到厭煩,你能否推薦任何關於Linq的內容? –

+0

是啊當然,我覺得這是有用的:http://www.codeproject.com/Articles/19154/Understanding-LINQ-C – Mafii

+0

也,你可能想看看LINQ到SQL,因爲你似乎使用實體框架? https://msdn.microsoft.com/en-us/data/ff728653.aspx – Mafii

0

通過觀察你的類結構,下面是簡單的代碼示例它只是爲您提供的蒐集的對象訂單明細爲訂單ID和用戶名:

var orderDetails = Orders.Select(x => x.OrderId = orderId && x.username="yourusename"); 

現在通過使用的foreach這些項目迭代。