我有一个实体框架模型,其中包含一个Assets表,如下所示:
public partial class Asset
{
public int Switch { get; set; }
public string Port { get; set; }
public string Name { get; set; }
public string Connection { get; set; }
public string Interface { get; set; }
public string ConnectionUser { get; set; }
public string ConnectionDate { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我试图只返回'名称'列.这是我用来执行此操作的代码:
// create the entity model for DB operations
acls3Entities entities = new acls3Entities();
List<Asset> assets = entities.Assets.ToList();
foreach (Asset asset in assets)
{ …Run Code Online (Sandbox Code Playgroud) 我有一个实现另一个超类的接口测试类.在测试类中,我有一个方法,它应该从数组列表返回一个对象; 首先它应该检查列表中的对象以查看它是否存在,如果不存在,它将返回null,否则它将返回找到的对象.这是我实现的代码:
public class ProductDBImpl implements ProductDB {
// field declarations
ArrayList<Product> products = new ArrayList<Product>();
@Override
public Product getProduct(int productId)
{
// TODO Auto-generated method stub
// create an iterator for the Arraylist
Iterator<Product> pritr = products.iterator();
while(pritr.hasNext())
{
Product pr = pritr.next();
if (pritr.next().getId() == productId)
{
return pr;
}
else
{
return null;
}
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是我收到的错误是:此方法必须返回Product类型的结果.
我的印象是,通过使用迭代器并循环遍历列表中的每个元素,每个元素都是一个类型Product.请帮我识别我的错误.