如何在 LINQ 查询中使用 .tag 并传递给另一个方法?

发布于 2024-11-09 04:42:24 字数 2095 浏览 0 评论 0原文

我正在尝试从查询构建结果对象,该查询从列表项上的标签获取其 where 子句。下面是构建列表的代码:

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)
    {
        item.BackColor = Color.Red;
    }
    else if (span.TotalHours >= 36 && span.TotalHours < 48)
    {
        item.BackColor = Color.Yellow;
    }
    else
    {
        item.BackColor = Color.Green;
    }
}

从这里开始,我想使用所选项目中标签的值,使用标签作为 WHERE 子句中的约束,从 DataContext 创建一个新对象:

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 是从 DataContext 创建的对象数据上下文。 Record_HoldData.HoldID 的类型为 int。我得到的错误是:

运算符“==”不能应用于“int”和“object”类型的操作数

当我显示 object.ToString() 时,它显示正确的值,但是当我尝试将其解析为 int 时,出现此错误:

无法隐式转换类型 'System.Linq.IQueryable' 到 'Coke_Hold_Database.Record_HoldData'。 存在显式转换(您是否缺少强制转换?

这很奇怪,因为如果我尝试转换它,我就不会缺少强制转换。我尝试了解析、转换和强制转换。所有这些都出现相同的错误。我有什么帮助吗?

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)
    {
        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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白馒头 2024-11-16 04:42:24

取消装箱存储在 Tag 属性中的 int

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

Unbox the int stored in the Tag property:

where a.HoldID == (int)lstvHolds.SelectedItems[0].Tag
梦醒灬来后我 2024-11-16 04:42:24

事实证明,这是让编译器知道在这种特殊情况下只有一个结果的问题。我添加了 Single() 并且它起作用了:

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();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文