数据模型问题

时间:2010-04-28 10:07:34

标签: asp.net asp.net-mvc linq

public ActionResult Index()
    {
        testEntities6 devicesEntities = new testEntities6();
        List<DevicesModel> devicesModel = new List<DevicesModel>();
        var device_query = from d in devicesEntities.device
             join dt in devicesEntities.devicetype on d.DeviceTypeId equals dt.Id
             join l in devicesEntities.location on d.Id equals l.DeviceId
             join loc in devicesEntities.locationname on l.LocationNameId equals loc.Id
                           where l.DeviceId == d.Id
                           select new {
                               //devices
                               d.Id,
                               d.DeviceTypeId,
                               d.SerialNumber,
                               d.FirmwareRev,
                               d.ProductionDate,
                               d.ReparationDate,
                               d.DateOfLastCalibration,
                               d.DateOfLastCalibrationCheck,
                               d.CalCertificateFile,
                               d.Notes,
                               d.TestReportFile,
                               d.WarrantyFile,
                               d.CertificateOfOriginFile,
                               d.QCPermissionFile,
                               d.Reserved,
                               d.ReservedFor,
                               d.Weight,
                               d.Price,
                               d.SoftwareVersion,
                               //devicetype
                               dt.Name,
                               dt.ArticleNumber,
                               dt.Type,
                               //location
                               l.StartDate, //AS LastStartDate,
                               l.LocationNameId,
                               //locationname
                               Loc_name = loc.Name //AS CarrentLocation
                           };

        foreach (var dv in device_query) {
            devicesModel.Add(new DevicesModel(
                //device
                DeviceTypeId = dv.DeviceTypeId, 
      /*Why I have this error: The name 'DeviceTypeId' does not exist in the current context*/
                ...
                ));

        }
        return View(devicesModel);
    }

型号:

public class DevicesModel
 {
//device
public int deviceTypeId;
public int id;
public int serialNumber;
public string firmwareRev;
public DateTime productionDate;
public DateTime reparationDate;
public DateTime dateOfLastCalibration;
public DateTime dateOfLastCalibrationCheck;
public int calCertificateFile;
public string notes;
public int testReportFile;
public int warrantyFile;
public int certificateOfOriginFile;
public int qCPermissionFile;
public int BReserved;
public string reservedFor;
public double weight;
public int price;
public string softwareVersion;
//devicetype
public string name;
public string qrticleNumber;
public string type;
//location
public DateTime startDate;
public int locationNameId;
//locationname
public string loc_name;

public DevicesModel(){}

//device
public int DeviceTypeId{get;set;}
public int Id { get; set; }
public int SerialNumber { get; set; }
public string FirmwareRev { get; set; }
public DateTime ProductionDate { get; set; }
public DateTime ReparationDate { get; set; }
public DateTime DateOfLastCalibration { get; set; }
public DateTime DateOfLastCalibrationCheck { get; set; }
public int CalCertificateFile { get; set; }
public string Notes { get; set; }
public int TestReportFile { get; set; }
public int WarrantyFile { get; set; }
public int CertificateOfOriginFile { get; set; }
public int QCPermissionFile { get; set; }
public int bReserved { get; set; }
public string ReservedFor { get; set; }
public double Weight { get; set; }
public int Price { get; set; }
public string SoftwareVersion { get; set; }
//devicetype
public string Name { get; set; }
public string ArticleNumber { get; set; }
public string Type { get; set; }
//location
public DateTime StartDate { get; set; }
public int LocationNameId { get; set; }
//locationname
public string Loc_name { get; set; }
}

2 个答案:

答案 0 :(得分:1)

因为您在linq“Select”子句中创建了一个匿名对象。我想你想要创建一个DevicesModel类的实例,例如

...
where l.DeviceId == d.Id
select new DevicesModel {
//devices
Id = d.Id,
...

答案 1 :(得分:1)

我认为您打算使用花括号而不是圆括号。

new DevicesModel 
{
   //device
   DeviceTypeId = dv.DeviceTypeId, 
   ...
}

或者您是否尝试在C#4中使用命名参数?