2012-03-22 96 views
2

新建MVC3剃鬚刀 - LINQ to SQL的已經花了一些時間去尋找如何:查找客戶ID通過匹配Customer.UserName以登錄用戶

匹配的loged上UsersName到Customer.UserName,然後選擇客戶.CustomerID爲用戶。

public ViewResult Index() 
    { 
     var CustomerID = from c in Customer 
      where c.UserName = "User.Identity.Name" 
      select c.CustomerID; 

     return View(); 
    } 

回答

3

只需使用的實際變量:

public ActionResult Index() 
    { 
     var CustomerID = from c in Customer 
      where c.UserName == User.Identity.Name //use double equals for comparison 
      select c.CustomerID; 

     return View(CustomerID); //or whatever your view is -- some typed object? 
    } 
1

您使用的 「User.Identity.Name」 是錯誤的。你需要使用屬性中的HttpContext

在一個更簡潔的形式,你可以說

public ViewResult Index() 
{ 
    var currentUser = Customer.Where(c => c.UserName == HttpContext.User.Identity.Name); 
    return View(currentUser); 
}