Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从另一个表获取ID_C#_.net_Linq - Fatal编程技术网

C# 从另一个表获取ID

C# 从另一个表获取ID,c#,.net,linq,C#,.net,Linq,我正在尝试将一个产品添加到我的数据库中,我需要从另一个表中获取类别ID,但是我不确定如何才能做到这一点 categoryId = Convert.ToInt32((from c in dc.Categories where c.Name == p.category select c.Id).FirstOrDefault()), 基本上,我有一个包

我正在尝试将一个产品添加到我的数据库中,我需要从另一个表中获取类别ID,但是我不确定如何才能做到这一点

         categoryId = Convert.ToInt32((from c in dc.Categories
                                 where c.Name == p.category
                                 select c.Id).FirstOrDefault()),
基本上,我有一个包含所有不同类别的表,我需要传递来自外部web服务的文本,然后它将点击DB并获取它所要使用的cat的ID

         categoryId = Convert.ToInt32((from c in dc.Categories
                                 where c.Name == p.category
                                 select c.Id).FirstOrDefault()),
以下是我的代码:

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,
        };
    }
}
         categoryId = Convert.ToInt32((from c in dc.Categories
                                 where c.Name == p.category
                                 select c.Id).FirstOrDefault()),
因此,基本上web服务中的类别正在传入category_1(例如),我需要将其传递给我的DB,以获取category_1的ID(比如它的1),然后将其插入到我的表中。

您需要使用,假设您从web服务获得的响应是有效的,而不是空的

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

FirstOrDefault/First/SingleOrDefault/Single接受谓词:

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

是LINQtoSQL还是实体框架,这个LINQtoEntities?你有导航属性映射吗?太棒了,干杯,Allieswell:有没有办法联系你,我还有一个问题你可以帮忙:)@thatuxguy:请随意发布一个新问题,并在这里发布一个链接。我试着回答:)-非常感谢:)不知道你对上述问题做了什么。你在接电话吗?因为很多东西似乎都被移除了:)