我想获取我在gridview中选择的产品的名称。 我有一个索引号,我可以用来在我的数据库中比较,但我无法选择属于该索引号的项目的名称。

ServiceReference1.ProductContext ctx = new ServiceReference1.ProductContext(new Uri("http://SERVER:5000/WcfDataService1.svc/"));

DbList = ctx.Products;
int index = ProductsList.SelectedIndex;

string name = DbList.XXXXXXX  // -> ?????????
.

我尝试过的一切都以例外内容。任何想法如何获取产品名称?

提前感谢。

有帮助吗?

解决方案 3

在rcl和lammmy的西蒙帮助我朝着正确的方向。谢谢!

所以我解决问题的解决方案是:

ServiceReference1.Product product = (ServiceReference1.Product)this.ProductsList.Items[productIndex];
string name = product.Name;
.

其他提示

类似的东西(这是空调,所以可能不是正确的):

Product product = DBList.Where<Product>((p) => p.Id == index);
.

这假设dblist是类型产品的列表,并且产品具有符合您要查找的索引的ID属性。

然后你可以做

string name = product.Name;
.

等等等。

你可以做

Product myProduct = ctx.Products.Find(index);
string name = myProduct.Name;
.

我不确定是否在DataService上下文中应用了此方法。

编辑:我是思考实体框架/ applicationdbcontext。我认为rcl的西蒙有Linq的答案。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top