Using an array in select expert, is it possible? - crystal-reports-2008

I currently have the following in select expert to select a bunch of fields:
{Order.OrderDate} in {?Start date} to {?End date} and
{Order.PostingCode} in ["j500", "j501", "j502"]
I am having to add from j500 all the way up to j713.
Is it possible to point it to a string array?
For example, in java:
int count = 0;
int fieldnumber = 500;
String[] fieldarray = new String[214];
while (count < 214){
System.out.println("j"+fieldnumber);
fieldarray[count] = "j"+fieldnumber;
fieldnumber++;
count++;
}
So in crystal I could just point crystal at the "fieldarray" string array and say use everything in there. Is that possible with crystal? If so, how?
Thanks guys.

why not use:
{Order.OrderDate} in {?Start date} to {?End date} and
{Order.PostingCode} >= "j500" and
{Order.PostingCode} <= "j713"

Related

Android: Connecting buttons to the java file through a for loop

I'm trying to create a chess game for Android and would like to avoid having to declare every button when they are all named so similarly. I tried to generate all of the A + Num button using this for loop.
int RowNum ;
for (RowNum = 1; RowNum < 8; RowNum++) {
String Position = "A" + RowNum;
Button Position = (Button)findViewById(R.id.Position);
}
But I have an error: Variable 'Position' is already defined in the scope.
I would really appreciate if someone could explain to me the best way to go about doing this.
Thanks in advance.
R.Id.A1, R.id.A2, etc. are identifiers and each of them is mapped to a single integer during compilation of R class. Thus, you cannot manipulate the 'R.id.sth' as a string because it will not compile properly.
A way to solve that is search for the string in resources dynamically with a code like this:
Button[] buttons=new Button[8];
for(int RowNum=0; RowNum<8; RowNum++) {
String buttonID = "A"+(RowNum+1);
int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
buttons[RowNum] = ((Button) findViewById(resID));
}
Alternatively, to avoid the time overhead of dynamic search you can add a resource array:
int[] butIds = {R.id.A1, R.id.A2,...};
Button[] buttons= new Button[8];
for(int RowNum=0; RowNum<8, RowNum++) {
buttons[RowNum]= ((Button) findViewById(butIds[RowNum]));
}
You can even store the resource array in XML form and retrieve it as a TypedArray.
You called your String and Button object both Position. Give them different names, e.g.
for (int RowNum = 1; RowNum < 8; RowNum++) {
String currentPosition = "A" + RowNum;
//Why doesn this not use the Position (now 'currentPosition' variable)?
Button currentButton = (Button)findViewById(R.id.Position);
}
Semantically I still don't get that piece of code because Position is not used within the argument of findViewById, to which you pass a static variable R.id.Position.

Replace for loop with LINQ

Is there is a LINQ expression equivalent to below code?
int noOfColumns = 10;
for (int i = 2; i <= noOfColumns+1; i++)
{
sql.AppendFormat(" [{0}{1}] [varchar]({2}),", "commodity", columnLength);
}
Enumerable.Range(2, noOfColumns+1)
.ToList()
.ForEach(i =>
sql.AppendFormat(" [{0}{1}] [varchar]({2}),", "commodity" columnLength));
But I don't think it looks better :-).
For what it's worth, the (compiling) Linq replacement would be:
IEnumerable<string> cols = Enumerable.Range(2, noOfColumns +1)
.Select(i => string.Format(" [{0}] [varchar]({1})", "commodity", columnLength));
string sql = string.Join(",", cols);
But i must admit that i have no idea what you're doing.
The parameters of Enumerable.Range are start and count. This means that it does not directly replace for loops.
for(var i = 5; i < 8; ++i) will produce the numbers from 5 to 7.
Enumerable.Range(5, 8) will produce the numbers from 5 to 12 (5+8-1).
To get the desired effect, Enumerable.Range(start, end - start) would need to be used.
Try This,
Enumerable.Range(2, noOfColumns).ToList().ForEach(r => { sql.AppendFormat(" [{0}{1}] [varchar]({2}),", "commodity" columnLength); });

Truncating a collection using Linq query

I want to extract part of a collection to another collection.
I can easily do the same using a for loop, but my linq query is not working for the same.
I am a neophyte in Linq, so please help me correcting the query (if possible with explanation / beginners tutorial link)
Legacy way of doing :
Collection<string> testColl1 = new Collection<string> {"t1", "t2", "t3", "t4"};
Collection<string> testColl2 = new Collection<string>();
for (int i = 0; i < newLength; i++)
{
testColl2.Add(testColl1[i]);
}
Where testColl1 is the source & testColl2 is the desired truncated collection of count = newLength.
I have used the following linq queries, but none of them are working ...
var result = from t in testColl1 where t.Count() <= newLength select t;
var res = testColl1.Where(t => t.Count() <= newLength);
Use Enumerable.Take:
var testColl2 = testColl1.Take(newLength).ToList();
Note that there's a semantic difference between your for loop and the version using Take. The for loop will throw with IndexOutOfRangeException exception if there are less than newLength items in testColl1, whereas the Take version will silently ignore this fact and just return as many items up to newLength items.
The correct way is by using Take:
var result = testColl1.Take(newLength);
An equivalent way using Where is:
var result = testColl1.Where((i, item) => i < newLength);
These expressions will produce an IEnumerable, so you might also want to attach a .ToList() or .ToArray() at the end.
Both ways return one less item than your original implementation does because it is more natural (e.g. if newLength == 0 no items should be returned).
You could convert to for loop to something like this:
testColl1.Take(newLength)
Use Take:
var result = testColl1.Take(newLength);
This extension method returns the first N elements from the collection where N is the parameter you pass, in this case newLength.

LINQ: Field is not a reference field

I've got a list of IQueryable. I'm trying to split this list into an array of IQueryable matching on a certain field (say fieldnum) in the first list...
for example, if fieldnum == 1, it should go into array[1]. I'm using Where() to filter based on this field, it looks something like this:
var allItems = FillListofMyObjects();
var Filtered = new List<IQueryable<myObject>(MAX+1);
for (var i = 1; i <= MAX; i++)
{
var sublist = allItems.Where(e => e.fieldnum == i);
if (sublist.Count() == 0) continue;
Filtered[i] = sublist;
}
however, I'm getting the error Field "t1.fieldnum" is not a reference field on the if line. stepping through the debugger shows the error actually occurs on the line before (the Where() method) but either way, I don't know what I'm doing wrong.
I'm farily new to LINQ so if I'm doing this all wrong please let me know, thanks!
Why don't you just use ToLookup?
var allItemsPerFieldNum = allItems.ToLookup(e => e.fieldnum);
Do you need to reevaluate the expression every time you get the values?
Why not use a dictionary?
var dictionary = allItems.ToDictionar(y => y.fieldnum);

LINQ QUery: Take () based upon textbox value

I am creating a gridview that will be populated based upon a linq statement, the sql is as follows:
SELECT TOP 10 IDDesc, UnitUserfield1, UnitUserfield2, ProductPercentage
FROM tblOnlineReportingCOMPLETEWeights
WHERE (MaterialLevel = 'Primary') AND (MaterialText = 'Paper')
ORDER BY ProductPercentage DESC
Now, what I would like to do is let the user specify the Top 10, so essentially it is a "Top x" this being defined in a textbox i.e. they type in 50 into the textbox, the linq query is executed and the gridview displays the top 50.
I understand that using Take is the area I want to look at, is this correct? Is this even possible?!
Any thoughts, muchly appreciated.
PS: apologies for asking thick questions, I am very new to all of this!
You are correct. Take user input and feed it to Take. That'll do.
int howMany = Convert.ToInt32 (HowManyTextBox.Value);
var queryResult = /*.....*/.Take (howMany);
int max = 0;
if (Int.TryParse(TextBox1.Text, out max)
{
var q = (from tbl where ... orderby ... desc).Take(max);
}
Along those lines
Thank you all so much, I went with the following:
{
ORWeightsDataClassesDataContext db = new ORWeightsDataClassesDataContext();
int max = 0;
if (int.TryParse(txtbxHowMany.Text, out max))
{
var queryV = db.tblOnlineReportingCOMPLETEWeights
.Where(x => x.MaterialLevel == "Primary" && x.MaterialText == "Paper")
.OrderByDescending(x => x.ProductPercentage).Take(max);
GridView1.DataSource = queryV;
GridView1.DataBind();
}
}
It works a treat.
Thank so so much, very grateful and now my site is finally coming together...I feel liek celebrating...pub anyone?!

Resources