Search code examples
c#winformsentity-frameworkentitywindows-forms-designer

Access to fields of list


I am trying to update this list but I can't access the fields of list :

var listevDetail = db.EvidenceDetail
                     .Get(p => p.EvidenceId == _EvidenceId)
                     .ToList();

for (int i = 0; i < dgEvidence.RowCount - 1; i++)
{
    listevDetail.EvidenceId = _EvidenceId;
    listevDetail.MerchandiseName = dgEvidence.Rows[i].Cells["Merchandise"].Value.ToString();
    listevDetail.weight = double.Parse(dgEvidence.Rows[i].Cells["Weight"].Value.ToString());
    listevDetail.Unit = dgEvidence.Rows[i].Cells["Unit"].Value.ToString();
    listevDetail.Description = dgEvidence.Rows[i].Cells["Description"].Value?.ToString();

    db.EvidenceDetail.Update(listevDetail);

    db.Save();
}

Solution

  • It's not clear what your "Get()" method is, but based on your example you are loading a collection of values (listevDetail) for a given "Evidence" while also passing a collection of rows. The error you are getting is because you're trying to update a List<Evidence> (Get().ToList()) as if it were somehow a single Evidence record, but then you are also parsing through a collection of whatever these dgEvidence data structure are. Either you really should have a list of Evidence records to match up against these dgRecords, or you mean to be updating a parent-child relationship under a given single Evidence record. (based on the use of an EvidenceId)

    This looks like there is some form of parent-child relationship you want to update, but you will need to provide the definitions for the Entities involved (Evidence and the potential child entities containing details like the MerchandiseName, Weight, Unit, etc.) as well as the data passed in.

    If Evidence has a collection of details of type EvidenceDetail as an example that you want to update by passing a set of data grid rows, then you'd be looking at something like:

    var evidence = _context.Evidences
        .Include(x => x.EvidenceDetails)
        .Single(x => x.EvidenceId == _evidenceId);
    
    for (int i = 0; i < dgEvidence.RowCount - 1; i++)
    {
        var detail = evidence.EvidenceDetails.SingleOrDefault(x => x.DetailId == dgEvidence.Rows[i].Cells["DetailId"].Value;
        if (detail == null)
        {  // TODO: Create a new Detail and add to evidence.EvidenceDetail...
        }
        else
        {
           detail.MerchandiseName = dgEvidence.Rows[i].Cells["Merchandise"].Value.ToString();
           detail.Weight = double.Parse(dgEvidence.Rows[i].Cells["Weight"].Value.ToString());
           detail.Unit = dgEvidence.Rows[i].Cells["Unit"].Value.ToString();
           detail.Description = dgEvidence.Rows[i].Cells["Description"].Value?.ToString();
        }
    }
    
    _context.SaveChanges();
    

    If rows could be added/removed from the provided set, you would need to handle those scenarios explicitly. The above example is a guess at a possible relationship you might need to consider but the actual implementation in your case will depend entirely on the data structure of your entities and what data you are passing into the method.

    If instead EvidenceId represents more of a "type" of evidence and you intend to bring back a collection of Evidence records for that ID, then fetch that list, but you need some means of marrying individual dgEvidence rows with resulting Evidence entities:

    var evidences = _context.Evidences
        .Where(x => x.EvidenceId == _evidenceId)
        .ToList();
    
    for (int i = 0; i < dgEvidence.RowCount - 1; i++)
    {
        var evidence = evidences.Single(x => x.Id == dgEvidence.Rows[i].Cells["Id"].Value;
        evidence.MerchandiseName = dgEvidence.Rows[i].Cells["Merchandise"].Value.ToString();
        evidence.Weight = double.Parse(dgEvidence.Rows[i].Cells["Weight"].Value.ToString());
        evidence.Unit = dgEvidence.Rows[i].Cells["Unit"].Value.ToString();
        evidence.Description = dgEvidence.Rows[i].Cells["Description"].Value?.ToString();
    }
    
    _context.SaveChanges();
    

    This requires having some form of actual identifying ID in your passed in collection to link to an expected Evidence row from the data.