所以我创建了一个包含字符串,整数和浮点数的类。
然后我在这些类型的main中声明了一个数组,并将该类型的对象读入其中
现在我需要在该数组中搜索特定值,如果该值匹配,则返回整个对象
我将如何做到这一点?
真的很难过
public class cdClass
{
private static string artist = null;
private static string genre = null;
private static string cdTitle = null;
private static float mSRP;
private static int stock;
private static int upc = 0;
//Following functions are public member methods
public void read_cd(string artist, string genre, string cdTitle, float mSRP, int stock, int upc)
{
//cdClass cd = null ;
System.Console.WriteLine("Enter Artist Name: ");
artist = Console.ReadLine();
System.Console.WriteLine("Enter CD Title: ");
cdTitle = Console.ReadLine();
System.Console.WriteLine("Enter Genre Type: ");
genre = Console.ReadLine();
System.Console.WriteLine("Enter Manufacturers Suggested Retal Price: ");
mSRP = float.Parse(Console.ReadLine());
System.Console.WriteLine("Enter UPC Number: ");
upc = int.Parse(Console.ReadLine());
System.Console.WriteLine("Enter Stock: ");
stock = int.Parse(Console.ReadLine());
//return cd;
}
public int get_upc()
{
return upc;
}
MAIN:
//Follwoing cod will initialize an array of Cd's
cdClass[] cdArray = new cdClass[20];
float taxRate = 0;
do
{
int i = 0;
cdClass current_cd = new cdClass();
current_cd.read_cd(artist, genre, cdTitle, mSRP, stock, upc);
cdArray[i] = current_cd;
i++;
} while (businesslogic.question() != 'Y');
buyer = inputfunctions.buyer();
int UPC = inputfunctions.get_upc();
for (int i = 0; i < 20; i++)
{
if (cdArray[i].get_upc() == UPC)
答案 0 :(得分:16)
您可以使用简单的LINQ扩展方法来搜索对象。
var foundItem = myArray.SingleOrDefault(item => item.intProperty == someValue);
以下是一些让您更熟悉的MSDN information regarding LINQ。
发布的代码编辑。
我首先想说的是,你看起来像是从一个不同的语言带来了一些范例,比如使用你的getter方法而不是使用.NET样式属性的java,这是你可能想要研究的东西。但是我已经根据您的具体案例制作了一个代码示例..
您可以替换块
for (int i = 0; i < 20; i++)
{
if (cdArray[i].get_upc() == UPC)
使用
cdClass foundCD = cdArray.SingleOrDefault(cd => cd.get_upc() == UPC);
或者使用BrokenGlass建议的Array.Find()
方法..
cdClass foundCD = Array.Find(cdArray, delegate(cdClass cd) { return cd.get_upc() == UPC); });
答案 1 :(得分:6)
Array.Find()
是LINQ的替代方案,特别是如果您只限于较旧的.NET版本:
var fooItem = Array.Find(myArray, item => item.fooProperty == "bar");
答案 2 :(得分:1)
class TheThing {
public int T1;
public float T2;
}
List<TheThing> list = new List<TheThing>();
// Populate list...
var instance = (from thing in list
where thing.T1 == 4
select thing).SingleOrDefault();
这假设您只有一个匹配T1 == 4
,如果您将拥有多个匹配,请执行以下操作:
var instances = from thing in list
where thing.T1 == 4
select thing;
答案 3 :(得分:0)
使用LINQ,如:
public class Car
{
public int ID { get; set; }
public int CarName { get; set; }
}
class Program
{
public IEnumerable<Car> GetCars
{
get { return MyDb.Cars; }
}
static void Main(string[] args)
{
Car myCar = GetCars.FirstOrDefault(x => x.ID == 5);
Console.WriteLine("ID: {0} | CarName {1}", myCar.ID, myCar.CarName);
}
}
http://msdn.microsoft.com/en-us/library/bb397926.aspx将为您提供必要的信息,帮助您开始使用LINQ。
答案 4 :(得分:0)
对于那些仍然没有&#34;卡住&#34;在.Net 2 ...
我的类数组结构......
wsStudentID == 'somevalue'
我想根据使用List<EmployerContactDetailsClass> OrgList = new List<EmployerContactDetailsClass>();
传递的索引值在此类中找到特定元素。
首先创建一个列表数组
EmployerContactDetailsClass OrgElem = new EmployerContactDetailsClass();
接下来创建一个节点元素
OrgElem.wsOrgID = reader["OrganisationID"].ToString(); //Org ID from database
OrgElem.wsContactID = reader["ContactID"].ToString(); //Contact ID from employer database
OrgElem.wsUserChoiceVerifier = (bool)reader["UserChoiceVerify"]; //boolean by default
if (reader["ContactGivenName"].ToString() != string.Empty && OrgElem.wsUserChoiceVerifier)
OrgElem.wsContactName = reader["ContactGivenName"].ToString() + " " + reader["ContactFamilyName"].ToString();
else
OrgElem.wsContactName = "TO WHOM IT MAY CONCERN";
OrgElem.wsContactEmail = reader["ContactEmail"].ToString(); //"support@tecnq.com.au";
OrgElem.wsEmployerEmail = reader["OrganisationEmail"].ToString(); //"support@tecnq.com.au";
//now add the elements into the array class
OrgList.Add(OrgElem);
从某些数据源构建列表(我的情况是Reader对象)
EmployerContactDetailsClass[] OrgListArray = (EmployerContactDetailsClass[])OrgList.ToArray();
转换列表&lt;&gt;进入一个数组[]
Delegate
使用String wsStudentID = 'FIND007'; //with respect to Roger Moore RIP
EmployerContactDetailsClass OrgElemFound = Array.Find(OrgListArray, ByStudentID(wsStudentID));
在数组中查找给定的StudentID。
//predicate method to return true or f if studentID matches the array list (used to pass into SendEmail)
private Predicate<EmployerContactDetailsClass> ByStudentID(string wsStudentID)
{
return delegate(EmployerContactDetailsClass OrgElem)
{
return OrgElem.wsStudentID == wsStudentID;
};
}
你的代码上的其他地方的Predicate方法......
function somefunc() {
PageFrame.goToTab('test', function(item) {
if(!item.search){
return false;
}
//else do some stuff
}, this);
}
window.onload = somefunc;
干杯!