Reputation: 1057
Alright, I have a brain block (pre-thanksgiving haze if you will). I have deconstructed an integer to discover what bits are set (this was set based upon selected listbox items). So if I have my integer value being 11 then bits 1,2,4 are set. So I have deconstructed my bit mask to get which bit is set. I can put that back into a list of integers or an integer array.
Here is my problem. I have a stored procedure that goes to the database and returns a list of a class. This class was constructed by me and contains integers, strings, basically w/e I need in the class. The class looks for records matching user entered criteria. I am adding the multiple selection list box (done) and generating a bit mask that returns the integer value of the bits that are set (done). I make the database call and get the list of transactions based upon user entered criteria. I do not want to rewrite/add to my stored proc because it is used else where a decent amount. I just want to apply some LINQ that will search for all records matching the selections in my list.
The list will return different types of TransactionTypes. I get the transaction types row number (14 transaction types, 14 bits, 1-14 are the row numbers I am identifying off of). I made a loop to to add the row number integer to an array of integers.
for (int i = 0; i <= //countOfSelections is 14; i++)
{
int num = 0;
num = (//int of decimal value constructed & (1 << i));
if (num != 0)
bits[i] = num;
}
I have a database call that makes var data the list of the class. Is there anyway to loop through using link and apply a loop within the .where clause based upon the selection? So where RowNumber in the class equals the row numbers in the bit[] ?
EDIT: Here is more code.
My list box has selections in it (14 types of transactions).
When the user ctrl+clicks the types of transactions and searches upon them, I add the transaction types row number that is selected to a list of integers. So if selection one two and four are selected, my list containts 1,2,4. I do not want to have 14 where statements or be passing around 14 values since this is hardcoded. (what if i add more to the db table then I have to change this elsewhere).
In order to prevent myself from having to do this I assemble a bit mask and get an integer value (decimal of the binary). I do this like so:
int total = 0;
//Building the bit mask
for (int c = 0; c < optsNum.Count(); ++c)
{
for (int i = 0; i <= transTypeCount; i++)
{
if ((i +1) == optsNum[c])
total += (1 << i);
}
}
return total;
Note that optsNum is my list of integers that contains the row numbers of the user selections. Also, transTypeCount is an integer that is the count of the possible selections in my list box. So again, if option 1,2,4 are selected. My returned total will equal 11.
I am then passing 11 to a method in some business logic. This method also receives some other user options (basic drop down boxes and such).
I have a class that contains information for transactions (contains quantities, products, names, and transaction types (my row numbers)). My database call returns a list of this class and it is stored in 'var data'. So 'data' is now a list of those classes.
In my method I now have data and need to filter data.TransactionID based upon the selected transaction types the user wants to see (this is within my decimal i made from the bit mask). So I need to deconstruct my bit mask to find out which bits are set. **I did this in the above code and bits[] array will contain an array of integers set. So again, 1,2,4.
I want to filter data to only get data where TransactionIDs in data are contained within my array bits[]. Is there anyway I can use LINQ to search through the class and only assemble a list of the class where the transaction IDs in that class equal the transaction IDs in my bits[] array? This would be a loop somehow...like loop through the bits[] and selected where transactionID = bits[i]. I am fairly new to LINQ and this is confusing me.
Upvotes: 0
Views: 441
Reputation: 1301
You can use a join:
var selectedTransactions =
from t in data
join transactionId in bits on t.TransactionID equals transactionId
select t;
Upvotes: 3