将 LINQ 查询对象存储为整数
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是您的第一个查询返回具有整数属性的对象集合,而不是单个整数。您可以将查询更改为:
或者,更干净地,IMO:
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:
Or, more cleanly, IMO:
tr
是一个集合。您必须调用tr.FirstOrDefault().Product_ID
。tr
is a collection. You have to calltr.FirstOrDefault().Product_ID
.我将更改您的 linq 查询以仅选择 s.Product_ID,而不是选择包含 ID 的新匿名对象。
您的第二个代码块可以是简单的
ev.Product_ID = tr.First();
I would change your linq query to select just the
s.Product_ID
instead ofselect new
ing a new anonymous object containing the ID.Your second code block could then be simply
ev.Product_ID = tr.First();
ev.Product_ID = tr.First().Product_ID;
ev.Product_ID = tr.First().Product_ID;