Xabarass
Xabarass

Reputation: 3

c# Search in structured list

I have a List based on struct

public struct MemoryLocation
{
    public byte address;
    public byte data;
}

public List<MemoryLocation> EEPromCurrList=new List<MemoryLocation>();

I would like to fill this list and after this search using one of the property, for example

MemoryLocation tmpMemCurr;
Random rnd = new Random();
for (int i=0;i<10;i++)
{
    tmpMemCurr.address=(byte)rnd.Next(i,100);
    tmpMemCurr.data=(byte)rnd.Next(i,10);
    EEPromCurrList.Add(tmpMemCurr);
}

Now The Goal should be to search in EEPromCurrList if there is a given address, for example 8 and have his value or index in list.

I need it because i have to compare two lists different lenght and check if the at same address they have same value.

Upvotes: 0

Views: 104

Answers (1)

Sumit raj
Sumit raj

Reputation: 831

once you are done filling the list, you can search it using

//search by address
var memoryLocation=tmpMemCurr.Where(x=>x.address==data to be searched).FirstOrDefault();
//search by data
var memoryLocation=tmpMemCurr.Where(x=>x.d==data to be searched).FirstOrDefault();

If available it'll give you corresponding memoryLocation else null.

I am not sure if you want index but if you need it,

var index=tmpMemCurr.FindIndex(x=>x.address==data to be searched); 
//returns 0-based index, else-1

If it still doesn't help you just clarify a bit with samples.

Upvotes: 2

Related Questions