Woocommerce.NET+;C#:从SKU获取产品ID
我正在用C#和Woocommerce.NET包装器制作一个小应用程序。 我想编辑产品,但我没有仅产品SKU的ID 从SKU编号获取产品ID的最简单方法是什么 我试过这个:Woocommerce.NET+;C#:从SKU获取产品ID,c#,woocommerce-rest-api,C#,Woocommerce Rest Api,我正在用C#和Woocommerce.NET包装器制作一个小应用程序。 我想编辑产品,但我没有仅产品SKU的ID 从SKU编号获取产品ID的最简单方法是什么 我试过这个: RestAPI rest = new RestAPI(url, key, secret); WCObject wc = new WCObject(rest); var products = wc.Product.GetAll().GetAwaiter().GetResult(); products.ForEach(p
RestAPI rest = new RestAPI(url, key, secret);
WCObject wc = new WCObject(rest);
var products = wc.Product.GetAll().GetAwaiter().GetResult();
products.ForEach(p => if p.sku = productSKU)
{
var productIDfromSKU = p.id;
}
编辑:我已经更改了代码,转换为int,现在可以工作了
foreach (var p in products)
{
if (p.sku == SKU)
{
IDfromSKU = Convert.ToInt32(p.id);
};
};
但问题是,我只得到了10种产品的清单——不是全部。
有这样的背景吗?
我的问题仍然是——有没有更直接的方法
编辑2:
我已经实现了你的答案,代码可以运行,但是速度非常慢。
约5000种产品上2-3分钟
我能做些什么来加快速度
编辑3:
对不起,没有做足够的测试-第二个答案很好
string SKU = "***wanted SKU***";
List<Product> products = new List<Product>();
Dictionary<string, string> pDic = new Dictionary<string, string>();
pDic.Add("sku", SKU);
int productIDfromSKU = 0;
string productNamefromSKU = "";
products = await wc.Product.GetAll(pDic);
if (products.Count > 0)
{
productIDfromSKU = Convert.ToInt32(products[0].id);
productNfromSKU = products[0].name;
}
string SKU=“***想要的SKU***”;
列表产品=新列表();
字典pDic=新字典();
pDic.添加(“sku”,sku);
int productIDfromSKU=0;
字符串PRODUCTNAME FROMSKU=“”;
产品=等待wc.Product.GetAll(pDic);
如果(products.Count>0)
{
productIDfromSKU=Convert.ToInt32(产品[0].id);
productNfromSKU=产品[0]。名称;
}
想想我的问题解决了
谢谢大家! RestAPI rest=new RestAPI(url、key、secret);
RestAPI rest = new RestAPI(url, key, secret);
WCObject wc = new WCObject(rest);
List<Product> products = new List<Product>();
Dictionary<string, string> dic1 = new Dictionary<string, string>();
dic1.Add("per_page", "100");
int pageNumber1 = 1;
dic1.Add("page", pageNumber1.ToString());
bool endWhile1 = false;
while (!endWhile1)
{
var productsTemp = await wc.Product.GetAll(dic1);
if (productsTemp.Count > 0)
{
products.AddRange(productsTemp);
pageNumber1++;
dic1["page"] = pageNumber1.ToString();
}
else
{
endWhile1 = true;
}
}
foreach (Product p in products)
{
if (p.sku == SKU)
{
IDfromSKU = Convert.ToInt32(p.id);
break;
};
}
WCObject wc=新的WCObject(rest);
列表产品=新列表();
字典dic1=新字典();
dic1.添加(“每页”、“100”);
int pageNumber1=1;
dic1.添加(“第页”,页码1.ToString());
bool endWhile1=假;
而(!endWhile1)
{
var productsTemp=wait wc.Product.GetAll(dic1);
如果(productsTemp.Count>0)
{
products.AddRange(productsTemp);
页码1++;
dic1[“页面”]=pageNumber1.ToString();
}
其他的
{
endWhile1=真;
}
}
foreach(产品中的产品p)
{
如果(p.sku==sku)
{
IDfromSKU=转换为32(p.id);
打破
};
}
RestAPI rest=new RestAPI(url、密钥、机密);
WCObject wc=新的WCObject(rest);
列表产品=新列表();
字典dic1=新字典();
dic1.添加(“每页”、“100”);
int pageNumber1=1;
dic1.添加(“第页”,页码1.ToString());
bool endWhile1=假;
而(!endWhile1)
{
var productsTemp=wait wc.Product.GetAll(dic1);
如果(productsTemp.Count>0)
{
products.AddRange(productsTemp);
页码1++;
dic1[“页面”]=pageNumber1.ToString();
}
其他的
{
endWhile1=真;
}
}
foreach(产品中的产品p)
{
如果(p.sku==sku)
{
IDfromSKU=转换为32(p.id);
打破
};
}
RestAPI rest=new RestAPI(url、密钥、机密);
WCObject wc=新的WCObject(rest);
字符串SKU=“***想要的SKU***”;
列表产品=新列表();
字典pDic=新字典();
pDic.添加(“sku”,sku);
int productIDfromSKU=0;
字符串PRODUCTNAME FROMSKU=“”;
产品=等待wc.Product.GetAll(pDic);
如果(products.Count>0)
{
productIDfromSKU=Convert.ToInt32(产品[0].id);
productNfromSKU=产品[0]。名称;
}
RestAPI rest=new RestAPI(url、密钥、机密);
WCObject wc=新的WCObject(rest);
字符串SKU=“***想要的SKU***”;
列表产品=新列表();
字典pDic=新字典();
pDic.添加(“sku”,sku);
int productIDfromSKU=0;
字符串PRODUCTNAME FROMSKU=“”;
产品=等待wc.Product.GetAll(pDic);
如果(products.Count>0)
{
productIDfromSKU=Convert.ToInt32(产品[0].id);
productNfromSKU=产品[0]。名称;
}
解释您为解决问题所做的工作会很有帮助。解释您为解决问题所做的工作会很有帮助。与其给出多个答案,为什么不更新原始答案?它有助于为他人保持清洁。还有,你知道林克吗?它使使用可枚举项变得容易得多。您可以使用products.FirstOrDefault()、products.First()或products.Single()代替产品[0],具体取决于您的代码所需的内容。在这种情况下,Single()看起来是正确的选择,因为您的代码只需要1个结果。与其生成多个答案,为什么不更新原始答案?它有助于为他人保持清洁。还有,你知道林克吗?它使使用可枚举项变得容易得多。您可以使用products.FirstOrDefault()、products.First()或products.Single()代替产品[0],具体取决于您的代码所需的内容。Single()在本例中看起来是正确的选择,因为您的代码需要1个且只有1个结果。
RestAPI rest = new RestAPI(url, key, secret);
WCObject wc = new WCObject(rest);
string SKU = "***wanted SKU***";
List<Product> products = new List<Product>();
Dictionary<string, string> pDic = new Dictionary<string, string>();
pDic.Add("sku", SKU);
int productIDfromSKU = 0;
string productNamefromSKU = "";
products = await wc.Product.GetAll(pDic);
if (products.Count > 0)
{
productIDfromSKU = Convert.ToInt32(products[0].id);
productNfromSKU = products[0].name;
}