在自定义类中创建方法,用于根据其他字段查找主ID



这应该是一个相当简单的问题。

背景:我有一个自定义类,它存储来自数据库的一些信息。它存储主键(ID)和描述/名称。我想通过搜索描述/名称返回主键。

这是我的第一个c#程序。

我试图使用LINQ在哪里做查找。对我来说,让这个方法成为我创建的自定义类的一部分是有意义的。你同意吗?

你建议我在哪里声明这个方法?在类中声明似乎是正确的事情
为什么"This.Where"语法不正常?-我想这是因为它不是一个列表。ClassTypeHere>

class DM_DistEquip
{
public int ixPanel; //stores ID_Key relation to panel name
public string sDescription; //stores name of panel
public int iPanelType; //store ID_Key for type of panel
public int distLookup(string pnl)
{
var dist = this.Where(x => x.sDescription == pnl).Select(y => y.ixPanel ).ToList(); //Lookup the primary key (ID) of the panel based of name of panel
return dist; //return ID of the Panel Name
}//end of method
}

我认为您可能需要一个外部包装器类,您可以将数据存储为集合。如果你愿意,你可以把distLookup()放进去。

public class OuterClass()
{
public List<DM_DistEquip> DistEquips {get;set;}
public int distLookup(string pnl)
{
var dist = DistEquips.Where(x => x.sDescription == pnl).Select(y => y.ixPanel ).ToList(); //Lookup the primary key (ID) of the panel based of name of panel
return dist; //return ID of the Panel Name
}//end

}

这应该有助于你更好地理解。

最新更新