开发者

How to use a .tag in a LINQ query and pass to another method?

I'm trying to build a result object from a query that gets its where clause from a tag on a list item. Here is the code that builds the list:

var holds = (from a in db.Record_HoldDatas
             join b in db.LUT_Flavors on a.Flavor equals b.ID
             where a.HoldStatus == "Open"
             orderby a.DateOpened descending
             select new { a.HoldID, a.Package, b.flavor, a.DateOpened }).ToList();

DateTime thenTime;
TimeSpan span;
foreach (var items in holds)
{
    ListViewItem item = new ListViewItem(items.HoldID.ToString());
    item.Tag = (int)items.HoldID;

    //determine age
    thenTime = (DateTime)items.DateOpened;
    span = DateTime.Now - thenTime;

    //add age
    item.SubItems.Add(span.TotalHours.ToString("0.0"));
    lstvHolds.Items.Add(item);

    //add flavor
    item.SubItems.Add(items.flavor.ToString());

    //add package
    item.SubItems.Add(items.Package.ToString());

    if (span.TotalHours >= 48)
    {
 开发者_如何学Python       item.BackColor = Color.Red;
    }
    else if (span.TotalHours >= 36 && span.TotalHours < 48)
    {
        item.BackColor = Color.Yellow;
    }
    else
    {
        item.BackColor = Color.Green;
    }
}

from here i want to use the value of the tag in the selected item to create a new object from the DataContext using the tag as a constraint in the WHERE clause:

private void lstvHolds_DoubleClick(object sender, EventArgs e)
{        
    //create instance of the selected record
    var existingRecord =  from a in db.Record_HoldDatas
                          where a.HoldID == lstvHolds.SelectedItems[0].Tag
                          select a;

    //open the hold window for viewing            
    frmNewFGHold viewHold = new frmNewFGHold("view", existingRecord);
    viewHold.Show();
}

Record_HoldData is the object created from the datacontext. Record_HoldData.HoldID is of type int. The error im getting is:

Operator '==' cannot be applied to operands of type 'int' and 'object'

When I display the object.ToString(), it shows the correct value, but when I try to parse it to int I get this error:

Cannot implicitly convert type 'System.Linq.IQueryable' to 'Coke_Hold_Database.Record_HoldData'. An explicit conversion exists (are you missing a cast?

which is bizarre because i wouldn't be missing a cast if i'm trying to convert it. I tried, parsing, converting and casting. All with the same error. I'm stumped. Any help?


Unbox the int stored in the Tag property:

where a.HoldID == (int)lstvHolds.SelectedItems[0].Tag


It turns out that it was a matter of letting the compiler know that theres only one result in this particular case. I added Single() and it worked:

int ID = (Int32)lstvHolds.SelectedItems[0].Tag;

//create instance of the selected record
Record_HoldData existingRecord = (Record_HoldData)
(from a in db.Record_HoldDatas
where a.HoldID == ID
select a).Single();

//open the hold window for viewing            
frmNewFGHold viewHold = new frmNewFGHold("view", existingRecord);
viewHold.Show();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜