我想知道如何使用asp.net mvc c#中的linq查询检索符合特定条件的值。如下面的代码所示,我使用过
然而,这似乎是检索它找到的第一个值,但是我希望检索匹配查询中指定条件的值。请记住,我已经声明了将该值保存为字符串的变量,但这使得无法检索值列表。我试图将选择行更改为select new { cas.Description, das.AssetInstanceID }).AsEnumerable;
,但我收到错误消息“无法将方法组分配给隐式类型的局部变量”
costCentreEntities = context.tbl_Organisation_CostCentre.Where(c => c.CostCentreID == userCostCentre && c.IsDeleted != true).OrderBy(c => c.CostCentreDesc);
var deviceDescription = (from cas in context.tbl_Organisation_CostCentreAssetSIM
join oc in context.tbl_Organisation_CostCentre
on cas.CostCentreID equals oc.CostCentreID
join das in context.tbl_Device_AssetSensor
on cas.AssetInstanceID equals das.AssetInstanceID
where das.ParentAssetInstanceID == null
select new { cas.Description, das.AssetInstanceID }).FirstOrDefault();
assetDescription = deviceDescription.Description;
assetId = deviceDescription.AssetInstanceID;
return costCentreEntities.ToList().Select(cc => new DeviceMetricItem(cc.CostCentreID, assetId, null, null, cc.ContractID, assetDescription, cc.CostCentreDesc, cc.tbl_Organisation_CostCentre1.Count)).ToList();
下面是我的dtos / class,其中我声明了保存所需值的变量,请注意描述和assetInstanceId变量,实际上应该能够保存一个或多个值,而不是只有一个< / p>
public DeviceMetricItem(int ? CostCentreID , int? AssetInstanceId, int? ParentCostCentreId, int? classificationId, int ? ContractID , string Description , string CostCentreDesc , int NoChildren = 0 )
{
costCentreId = CostCentreID.HasValue ? CostCentreID.Value:0;
assetInstanceId = AssetInstanceId.HasValue ? AssetInstanceId.Value : 0;
parentCostCentreId = ParentCostCentreId.HasValue ? ParentCostCentreId.Value : 0;
classificationId = classificationId.HasValue? classificationId.Value:0;
ContractID = ContractID.HasValue ? ContractID.Value:0;
description = Description;
costCentreDesc = CostCentreDesc ?? string.Empty;
noChildren = NoChildren ;
}
public int costCentreId { get; set; }
public string costCentreDesc { get; set; }
public int assetInstanceId { get; set; }
public int parentCostCentreId { get; set; }
public string description { get; set; }
public int noChildren { get; set;}
public int contractId { get; set; }