Note that there are some explanatory texts on larger screens.

plurals
  1. POC# LINQ: How to check object value of a listbox item and set color accordingly
    text
    copied!<p>I was wondering if this was possible. Here is the code that populates a listbox:</p> <pre><code>var allHolds = from a in db.Record_HoldDatas join f in db.LUT_Flavors on a.Flavor equals f.ID into flavorNames where a.HoldStatus == "Open" from grouped in flavorNames select new { a.HoldID, a.DateOpened, grouped.flavor, a.NonConformingItem, Caption = a.HoldID + " - " + a.Package + " " + grouped.flavor }; lbxHoldCaption.DataSource = allHolds; lbxHoldCaption.DisplayMember = "Caption"; </code></pre> <p>what I want to do is write a loop that goes through each object in the listbox and uses a couple values to do some math. If the math result is a certain value, then I want it to change that item's background color.</p> <p>I'm not sure how to use a foreach loop with this. Since the listbox is actually bound to a reference full of anonymous types</p> <p>More specifically, this is what im doing: One of the values in the object returned from the query is datetime "DateOpened" I want to subtract todays datetime with that to determine the hours elapsed. i was using something like this</p> <pre><code>DateTime now = DateTime.Now; DateTime then = //retrieved from query object TimeSpan span = now - then; </code></pre> <p>I was hoping to do this whole thing with something like</p> <pre><code>foreach(Object obj in allHolds) { then = obj.DateOpened; span = now - then; if(span.TotalHours &gt;= 48) { obj.BackColor = Color.Red; } } </code></pre> <p>but obviously this doesnt work because the referencing is all borked. Can someone help me out?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload