我继承的数据库有很多写得不好的存储过程,我不允许更改,因为它们被用在其他程序中,并且有太多要重写它们。我试图通过使用Linq GroupBy摆脱重复,但我得到的错误是“模型”不包含该字段的定义,当然,它确实如此。我确信这是我想念的简单事。非常感谢任何帮助!
这是我的代码:
public async Task<ActionResult> GetIncidentColumns()
{
int questionClusterID = 25;
var getcolumns = await CommonClient.GetIncidentColumnsForClusterID(questionClusterID);
var columns = getcolumns.GroupBy(c => c).Select(grp => grp.IncidentColumnID).ToArray();
return Json(columns, JsonRequestBehavior.AllowGet);
}
以下是模型:
public class IncidentColumnsModel
{
public int IncidentTabulationID { get; set; }
public int IncidentColumnID { get; set; }
public string vchColumnReference { get; set; }
public Nullable<int> ColumnNameID { get; set; }
public Nullable<byte> intSequence { get; set; }
public string vchHeaderCssClass { get; set; }
public string vchColumnHeaderText { get; set; }
public string vchToolTip { get; set; }
public string vchColumnCssClass { get; set; }
public Nullable<byte> intHeaderRow { get; set; }
public Nullable<bool> bitStartHeaderTextAboveColumn { get; set; }
public string vchHeaderTextAboveColumn { get; set; }
public Nullable<byte> intHeaderTextAboveColumnCount { get; set; }
public string vchHeaderTextAboveColumnColor { get; set; }
public Nullable<byte> intColumnPrecicion { get; set; }
public Nullable<byte> intColumnScale { get; set; }
public Nullable<bool> bitCalculatedColumn { get; set; }
public Nullable<bool> bitAllowDataEntry { get; set; }
public Nullable<bool> bitIncludeInSummaryRow { get; set; }
public Nullable<byte> ColumnSummaryTypeID { get; set; }
public Nullable<byte> intFormulaNumeratorASeqNum { get; set; }
public Nullable<byte> intFormulaNumeratorBSeqNum { get; set; }
public Nullable<byte> intFormulaNumeratorCSeqNum { get; set; }
public Nullable<byte> intFormulaNumeratorDSeqNum { get; set; }
public Nullable<byte> intFormulaNumeratorESeqNum { get; set; }
public Nullable<decimal> decFormulaNumeratorMultiplier { get; set; }
public Nullable<bool> bitThisIsARatio { get; set; }
public Nullable<byte> intFormulaDenomeratorFSeqNum { get; set; }
public Nullable<byte> intFormulaDenomeratorGSeqNum { get; set; }
public Nullable<byte> intFormulaDenomeratorHSeqNum { get; set; }
public Nullable<byte> intFormulaDenomeratorISeqNum { get; set; }
public Nullable<byte> intFormulaDenomeratorJSeqNum { get; set; }
public Nullable<decimal> decFormulaDenomeratorMultiplier { get; set; }
public Nullable<bool> bitFormulaValidate { get; set; }
public string vchValidateOnFormulaText { get; set; }
public Nullable<byte> intValidateGTESeqNum { get; set; }
public Nullable<decimal> decValidateGTEMultiplier { get; set; }
public Nullable<byte> intValidateLTSeqNum { get; set; }
public Nullable<decimal> decValidateLTMultiplier { get; set; }
public Nullable<bool> bitRangeValidate { get; set; }
public string vchValidateOnRangeText { get; set; }
public Nullable<decimal> decRangeValidGTE { get; set; }
public Nullable<decimal> decRangeValidLT { get; set; }
public Nullable<bool> bitRequiredField { get; set; }
public string vchAssociatedDetailColumn { get; set; }
public string vchAssociatedSummaryColumn { get; set; }
public Nullable<byte> intColumnPositionFromLeft { get; set; }
public Nullable<byte> intValidateGTESeqNumB { get; set; }
public Nullable<byte> intValidateGTESeqNumC { get; set; }
public Nullable<byte> intValidateLTSeqNumB { get; set; }
public Nullable<byte> intValidateLTSeqNumC { get; set; }
public Nullable<bool> bitRolling12MonthCalculation { get; set; }
public Nullable<bool> bitRolling4QuarterCalculation { get; set; }
public Nullable<byte> intSummaryFormulaNumeratorASeqNum { get; set; }
public Nullable<byte> intSummaryFormulaNumeratorBSeqNum { get; set; }
public Nullable<byte> intSummaryFormulaNumeratorCSeqNum { get; set; }
public Nullable<byte> intSummaryFormulaNumeratorDSeqNum { get; set; }
public Nullable<byte> intSummaryFormulaNumeratorESeqNum { get; set; }
public Nullable<decimal> decSummaryFormulaNumeratorMultiplier { get; set; }
public Nullable<bool> bitSummaryThisIsARatio { get; set; }
public Nullable<byte> intSummaryFormulaDenomeratorFSeqNum { get; set; }
public Nullable<byte> intSummaryFormulaDenomeratorGSeqNum { get; set; }
public Nullable<byte> intSummaryFormulaDenomeratorHSeqNum { get; set; }
public Nullable<byte> intSummaryFormulaDenomeratorISeqNum { get; set; }
public Nullable<byte> intSummaryFormulaDenomeratorJSeqNum { get; set; }
public Nullable<decimal> decSummaryFormulaDenomeratorMultiplier { get; set; }
}
答案 0 :(得分:0)
。GroupBy()返回IEnumerable<IGrouping<TKey, TSource>>
类型的对象。
IGrouping是否具有名为IncidentColumnID
?
答案 1 :(得分:0)
您只需要一列ID,因此您应该在IncidentColumnID
上进行分组,然后通过以下方式在IncidentColumnID
上进行投影:
var columns = getcolumns.GroupBy(c => c.IncidentColumnID)
.Select(grp => grp.First())
.ToArray();