将 LINQ 查询对象存储为整数

发布于 2024-10-30 08:06:59 字数 759 浏览 0 评论 0原文

我有一个 LINQ2SQL 查询:

 //Pulling the Product_ID from the PlanMaster Table from WebEnroll DB!
                var tr = from s in dt.PlanMasters
                         where s.PlanName == productName
                         select new
                         {
                             s.Product_ID
                         };

这正在拉取 Product_ID,效果很好。现在我想将一条记录添加到另一个 LINQ 语句中:

                CommissionsV2DataContext cv = new CommissionsV2DataContext();
                Entity_Product_Point ev = new Entity_Product_Point();
                ev.Entity_ID = getEntity;
                ev.Product_ID = tr.; ?????

我想将从 tr (即 Product_ID)获取的变量存储到 ev.Product_ID。

我应该如何将对象转换为 INT?谢谢你!

I have a LINQ2SQL Query:

 //Pulling the Product_ID from the PlanMaster Table from WebEnroll DB!
                var tr = from s in dt.PlanMasters
                         where s.PlanName == productName
                         select new
                         {
                             s.Product_ID
                         };

This is pulling Product_ID which is working good. Now I want to ADD a record into another LINQ statement which is here:

                CommissionsV2DataContext cv = new CommissionsV2DataContext();
                Entity_Product_Point ev = new Entity_Product_Point();
                ev.Entity_ID = getEntity;
                ev.Product_ID = tr.; ?????

I want to store the variable which I am getting from tr (that is Product_ID) to ev.Product_ID.

How should I convert an object to an INT? Thank you!

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

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

发布评论

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

评论(4

醉南桥 2024-11-06 08:06:59

问题是您的第一个查询返回具有整数属性的对象集合,而不是单个整数。您可以将查询更改为:

var tr = (
    from s in dt.PlanMasters
    where s.PlanName == productName
    select s.Product_ID).First();

或者,更干净地,IMO:

var tr = dt.PlanMasters.First(s => s.PlanName == productName).Product_ID;

The problem is that your first query returns a collection of objects with an integer property, not a single integer. You can change your query to this instead:

var tr = (
    from s in dt.PlanMasters
    where s.PlanName == productName
    select s.Product_ID).First();

Or, more cleanly, IMO:

var tr = dt.PlanMasters.First(s => s.PlanName == productName).Product_ID;
英雄似剑 2024-11-06 08:06:59

tr 是一个集合。您必须调用 tr.FirstOrDefault().Product_ID

tr is a collection. You have to call tr.FirstOrDefault().Product_ID.

本王不退位尔等都是臣 2024-11-06 08:06:59

我将更改您的 linq 查询以仅选择 s.Product_ID,而不是选择包含 ID 的新匿名对象。

 var tr = from s in dt.PlanMasters
          where s.PlanName == productName
          select s.Product_ID;

您的第二个代码块可以是简单的 ev.Product_ID = tr.First();

  CommissionsV2DataContext cv = new CommissionsV2DataContext();
  Entity_Product_Point ev = new Entity_Product_Point();
  ev.Entity_ID = getEntity;
  ev.Product_ID = tr.First();

I would change your linq query to select just the s.Product_ID instead of select newing a new anonymous object containing the ID.

 var tr = from s in dt.PlanMasters
          where s.PlanName == productName
          select s.Product_ID;

Your second code block could then be simply ev.Product_ID = tr.First();

  CommissionsV2DataContext cv = new CommissionsV2DataContext();
  Entity_Product_Point ev = new Entity_Product_Point();
  ev.Entity_ID = getEntity;
  ev.Product_ID = tr.First();
小霸王臭丫头 2024-11-06 08:06:59

ev.Product_ID = tr.First().Product_ID;

ev.Product_ID = tr.First().Product_ID;

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