2012-09-22 37 views
0

我想要做的是从我的数据库中获取特定值并将其解析到视图中以将其显示在表中。 我有和The model item passed into the dictionary is of type .... {Models.App} but what I passed is {Models.App, System.Double}一样的问题,并试图以类似的方式解决我的问题。在linq查询中给匿名类型键入

public Vehicle manur_name {get; set;} 
public Vehicle manur_date { get; set; } 
public Vehicle daily_hire_rate { get; set; } 

但后来当我在L​​INQ查询使用它在我的控制器它给了我和错误:所以基本上我在具有模型创建的类。

var s = from veh in db.Vehicles 
join c in db.VehicleCategories on veh.vehicle_category_code equals c. 
join m in db.Models on veh.model_code equals m.model_code 
join man in db.Manufacturers on veh.manufacturer_code equals man.manufacturer_code 
where c.vehicle_category_description == cat && m.body_style == b       
      select new CarClass { 
          manur_name = man.manufacturer_name, 
          manur_date = m.model_code, 
          daily_hire_rate = veh.daily_hire_rate}; 

它给我的错误:

error:Cannot implicitly convert type 'decimal' to 'carRentalMVC.Models.Vehicle' Cannot implicitly convert type 'string' to 'carRentalMVC.Models.Vehicle' Cannot implicitly convert type 'string' to 'carRentalMVC.Models.Vehicle'

这是我在控制装置固定问题后:

 CarRentalDatabaseDataContext db = new CarRentalDatabaseDataContext(); 
    public ActionResult Index(String cat, String b, String sort) 
    { 
     String day_hire = "Daily hire rate"; 
     String man_date = "Manufacturing date"; 
     string man_n = "Manufacturer's name"; 

     var s = db.Vehicles.Where(c => c.Model.body_style == b) 
      .Select(u => new CarClassViewModel { 
       manur_name = u.Manufacturer.manufacturer_name, 
       model_code = u.model_code, 
       daily_hire_rate = u.daily_hire_rate, 
       manur_date = u.manufacturing_date 
      }) 
      .Distinct(); //I don't need repeating data 

     //this is for sorting them, but I haven't implemented this in the view yet. 
     if (sort == man_n){s = s.OrderBy(c=>c.manur_name);} 
     else if (sort == man_date){s = s.OrderBy(c =>c.manur_date);} 
     else if (sort == day_hire){s = s.OrderBy(c => c.daily_hire_rate);} 
     else{s = s.OrderBy(c => c.daily_hire_rate);} 

     var v = s.Select(u => new CarClassViewModel 
     { 
      manur_name = u.manur_date, 
      model_code = u.model_code, 
      daily_hire_rate = u.daily_hire_rate, 
     }); //this is for returning values I need 

     return View(v); //I've tried View(v.ToList()) as well 
    } 

现在,这将返回我的表中的空行。这是否重要,我不解析数据的方法?

回答

4

I'm not sure what I'm doing wrong.

您的数据模型非常混乱。即使在离开的命名约定不谈,看看你的属性:

public Vehicle manur_name {get; set;} 
public Vehicle manur_date { get; set; } 
public Vehicle daily_hire_rate { get; set; } 

汽车的制造商名称是不是车辆 - 这很可能是一个字符串。一辆车的制造日期不是一辆车 - 它很可能是DateTime。汽车的每日租用率不是一辆汽车 - 很可能是decimal。所以,你的模型应该是这样的:

public string ManufacturerName { get; set; } 
public DateTime ManufacturingDate { get; set; } 
public decimal DailyHireRate { get; set; } 

让您的数据模型正确,一切应成为很多简单。话虽如此,这看起来很可疑太:

manur_date = m.model_code 

A“日期”和一个“代码”不健全的一样的东西给我。

+0

谢谢))我发现分配给变量的类型不正确。现在,我的代码中的所有东西都编译完成了,但它并没有真正返回任何东西......我应该如何解析视图? –

+0

@NurkaKindova:很难知道你做错了什么 - 请在新问题中提供更多细节。 (你有很多的连接和过滤 - 任何一部分可能会过滤你不想要的数据。) –

+0

好的,我修改了这个问题并添加了我目前拥有的代码。你能看看吗? –