gpt4 book ai didi

c# - 从另一个表中获取 ID

转载 作者:行者123 更新时间:2023-11-30 22:27:38 24 4
gpt4 key购买 nike

我正在尝试将产品添加到我的数据库中,我需要从另一个表中获取类别 ID,但我不确定该怎么做?

基本上,我有一个包含我所有不同类别的表格,我需要从我的外部网络服务传递文本,然后它将访问数据库并获取它应该在其中的猫的 ID。

下面是我的代码:

using (aboDataDataContext dc = new aboDataDataContext())
{
var match = (from t in dc.tweProducts
where t.sku == productSku
select t).FirstOrDefault();

if (match == null)
{
tweProduct tprod = new tweProduct()
{
sku = p.productcode,
categoryId = Convert.ToInt32(catid),
title = p.name,
brand = p.manufacturer,
description = p.description,
twePrice = p.price,
weight = decimal.TryParse(p.size, out weight) == true ? (int?)weight : null,
size = decimal.TryParse(p.size, out weight) == true ? (int?)weight : null,
contry = p.country,
region = p.region,
vintage = int.TryParse(p.vintage, out vintage) == true ? (int?)vintage : null,
strength = p.strength,
bottler = p.bottler,
age = int.TryParse(p.age, out age) == true ? (int?)age : null,
caskType = p.casktype,
series = p.series,
flavour = p.flavour,
stock = p.freestock,
tweUpdated = false,
stockUpdated = false,
priceUpdated = false,
dataUpdated = false,
imgPublished = false,
lastUpdated = DateTime.Now,
};
}
}

基本上,来自网络服务的类别在 Category_1 中传递(例如),我需要将其传递到我的数据库,以获取 Category_1 的 ID(比如它的 1),然后将其插入到我的表中。

最佳答案

您需要使用 FirstOrDefault() ,假设您从您的网络服务获得有效的而非空的响应。

         categoryId = Convert.ToInt32((from c in dc.Categories
where c.Name == p.category
select c.Id).FirstOrDefault()),

关于c# - 从另一个表中获取 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11191955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com