Reputation: 53
I have LINQ statement like this :
for (int i = 1; i < a + 1; i++)
{
sumBubblee = dtErrCode.AsEnumerable()
.Where(r => (r.Field<string>("ErrCode") == "000000FB") || (r.Field<string>("ErrCode") == "000000FF") || (r.Field<string>("ErrCode") == "000004FB") || (r.Field<string>("ErrCode") == "000004FF") || (r.Field<string>("ErrCode") == "000006FF"))
.Sum(r => Convert.ToInt32(r.Field<string>(i)));
rowBubble[i] = sumBubblee;
}
From the LINQ statement above, we know that I created 5 where condition statically.
What I am looking for: Is it possible to have a dynamically where clause?
I plan to insert all the where clause list in notepad file and then the LINQ read those Where ("ErrCode")
from list in the notepad. And also it is easy if we want to add new where condition in notepad.
Any help would be appreciated. Thank you
Upvotes: 0
Views: 101
Reputation: 34947
I came up with the following
var sumBubblee = dtErrCode.AsEnumerable()
.Where(r => whatImLookingFor.Contains(Convert.ToInt32(r.Field<string>("ErrCode"), 16)))
.Sum(r => Convert.ToInt32(r.Field<string>(i)));
And the test:
class R
{
public string Error { get; set; }
public int[] Fields {get; set;}
public string Field<T>(string s) => Error;
public string Field<T>(int i) => Fields[i].ToString();
}
static void Main(string[] args)
{
var dtErrCode = new List<R>() {
new R { Error = "000000FB", Fields = new int[]{0,444, 0} },
new R { Error = "000000FF", Fields = new int[]{0,222, 0} },
new R { Error = "000004FF", Fields = new int[]{0,0, 111} },
};
var whatImLookingFor = new [] { 0xFB, 0xFF, 0x4FB, 0x4FF, 0x6FF};
var rowBubble = new int[3];
var a = 2;
for (int i = 1; i < a + 1; i++) // I'm not sure why you're starting from 1 and not from 0
{
var sumBubblee = dtErrCode.AsEnumerable()
// .Select(r => new { ErrCode = r.Field<string>("ErrCode") }, I = )
// .Select(s => )
.Where(r => whatImLookingFor.Contains(Convert.ToInt32(r.Field<string>("ErrCode"), 16)))
.Sum(r => Convert.ToInt32(r.Field<string>(i)));
rowBubble[i] = sumBubblee;
}
//rowBubble [0,666,111]
}
Upvotes: 0
Reputation: 629
If you want to stick with the approach of pulling those names from a text file, then you could add them to a text file...
...and read the file and check for your error codes like this (I used spaces, but you could use commas or any other delimiter you want to - just be sure to set the delimiter in .Split()
in the code below):
// read the error codes from your text file
var fileErrorCodes = File.ReadAllText(@"C:\path\to\your\file\errorCodes.txt");
// convert them to a string array (this is where you would use
// e.g. .Split(',') if you choose to separate your error codes with a comma instead of a space
string[] errorCodes = fileErrorCodes.Split(' ');
// Now check if your errorCodes array contains any of the "ErrorCode" values in your dataset.
var result = dataset.AsEnumerable().Where(x => listOfErrorCodes.Contains(x.Field<string>("ErrorCode")));
Upvotes: 0
Reputation: 335
You can read the file and add it to a list. Imagine 'myCustomErrorList' on example.
Your 'where' clause would look like this:
.Where(r => myCustomErrorList.Any(ce => ce == r.Field<string>("ErrCode")))
Upvotes: 2