This code works correctly to make a web service call:
int numberOfGuests = Convert.ToInt32(search.Guest);
var list = new List<Guest>();
Guest adult = new Guest();
adult.Id = 1;
adult.Title = "Mr";
adult.Firstname = "Test";
adult.Surname = "Test";
list.Add(adult);
Guest adult2 = new Guest();
adult2.Id = 2;
adult2.Title = "Mr";
adult2.Firstname = "Test";
adult2.Surname = "Test";
list.Add(adult2);
Guest[] adults = list.ToArray();
How do I build the list dynamically using the numberofguests variable to create the list? The output has to match the output shown exactly else the web service call fails, so adult.id = 1, adult2.id = 2, adult3.id = 3, etc...
Do you know about loops?
for (int i = 1; i <= numberofGuests; i++) {
var adult = new Guest();
adult.Id = i;
adult.Title = "Mr";
adult.Firstname = "Test";
adult.Surname = "Test";
list.Add(adult)
}
This runs the code within the loop once from 1 to numberOfGuests, setting the variable i to the current value.
The Linq way :-)
var list = (from i in Enumerable.Range(1, numberOfGuests)
select new Guest
{
Id = i,
Title = "Mr.",
Firstname = "Test",
Surname = "Test"
}).ToList();
You need a for loop. Or, better yet, a decent C# book -- these are really basics of C#.
Are you asking how to display a list dynamically? I'm not really sure what the question here is about, as the other answers say if you know the value of numberofGuests then you can just use a loop to go through your list.
I suspect you are wondering how to obtain this information in the first place, am I right? If you want to dynamically add controls to a page (your previous post suggest this was ASP.Net I think?), so that you only display the correct number of controls then take a look at these related questions:
Dynamically adding controls in ASP.NET Repeater
ASP.NET - How to dynamically generate Labels
Related
I am trying to build a KQL Query using KeywordQuery.
I have some managed properties columns that I want to display in the result table but it's not working for me.
here is the code:
oKeywordQuery = new KeywordQuery(oSite);
oKeywordQuery.SelectProperties.Clear();
oKeywordQuery.QueryText = sQuery;
oKeywordQuery.KeywordInclusion = KeywordInclusion.AllKeywords;
oKeywordQuery.StartRow = 0;
oKeywordQuery.RowLimit = 500;
oKeywordQuery.EnableNicknames = true;
oKeywordQuery.EnablePhonetic = true;
oKeywordQuery.TrimDuplicates = false;
oKeywordQuery.SelectProperties.Add("IsDocument"); //This one as a test I was able to display but no managed properties
foreach (string sDisplayField in oDisplayFields)
{
oKeywordQuery.SelectProperties.Add(sDisplayField);
}
oSearchExecutor = new SearchExecutor();
oResultTableColl = oSearchExecutor.ExecuteQuery(oKeywordQuery);
var oResultTable = oResultTableColl.Filter("TableType", KnownTableTypes.RelevantResults);
oRTable = oResultTable.FirstOrDefault();
I am able to get results but no managed properties columns are shown.
What seems to be the problem?
Eventually I figured out that I had WHITE SPACES in the properties string.
For example:
oKeywordQuery.SelectProperties.Add(" MyPropTitle ");
Should be:
oKeywordQuery.SelectProperties.Add("MyPropTitle");
Can someone show me a simple way to assign variables.
I have many variables and not really know how to do, whether it be possible to use a loop?`
public void SwappingPlaces1()
{
Section_1[0] = Receiver_1[0];
Section_1[1] = Receiver_2[0];
Section_1[2] = Receiver_3[0];
Section_1[3] = Receiver_4[0];
Section_1[4] = Receiver_5[0];
Section_1[5] = Receiver_6[0];
Section_1[6] = Receiver_7[0];
Section_1[7] = Receiver_8[0];
Section_1[8] = Receiver_9[0];
Section_1[9] = Receiver_10[0];
Section_1[10] = Receiver_11[0];
Section_1[11] = Receiver_12[0];
Section_1[12] = Receiver_13[0];
Section_1[13] = Receiver_14[0];
Section_1[14] = Receiver_15[0];
Section_1[15] = Receiver_16[0];
Section_1[16] = Receiver_17[0];
Section_1[17] = Receiver_18[0];
Section_1[18] = Receiver_19[0];
Section_1[19] = Receiver_20[0];
Section_1[n] = Receiver_n[0];
...
}
You need to use reflection to get the names of the properties/fields by their name. Assuming Reciever_n is a property:
var properties = this.GetType().GetProperties();
for(int i = 0; i < n; i++)
{
var p = properties.Single(x => x.Name == "Receiver_" + i);
var value = p.GetValue(this, new object[] { 0 });
}
First you get all the properties defined on the type. Now you loop your list and get that single property with the name Receiver_ plus the current index.
Finally you invoke that property on the instance and provide the index of the indexed property (which is equal to zero here).
EDIT: However having so many properties with equal name and type seems a design-flaw, you should consider your actual design.
Thus a better appraoch might be to have just one single two-dimensional array Receiver.
You could add reference variables to an array and iterate over them. This is mostly usefull, clearing gui controls, like textboxes/labels etc.
var variables = new[] { Receiver_1, Receiver_2, Receiver_3, Receiver_4,
Receiver_5, Receiver_6 };
for(int i=0; i<Section_1.Length;i++)
Section_1[i] = variables[i][0];
But an index out of bounds is easely created
You could also use reflection.
List<ListOrRecordRef> List = new List<ListOrRecordRef>();
ListOrRecordRef RecordRefItem = new ListOrRecordRef();
RecordRefItem.name = "American Express";
RecordRefItem.internalId = "898";
RecordRefItem.typeId = "394";
List.Add(RecordRefItem);
rec.customFieldList = List.ToArray();
WriteResponse response = service.add(rec);
The code is used to add multiselect option of vendor. ex : american express
First of all like Heinz Siahaan said: 'List' is a keyword in C# so you can't create variable with this name.
Second:
ListOrRecordRef RecordRefItem = new ListOrRecordRef();
I'm not sure but name of this method suggest that this line of code creates list of records not one item so you can't use something like this:
RecordRefItem.name = "American Express";
but you should try :
RecordRefItem[i].name = "American Express";
where i is and index of element, but before access it you must create it
found a way its working fine://Note that for multi select option to set we have to take two class:ListOrRecordRef mention the id of the 898:American express&
//SelectCustomFieldRef to mention the field
ListOrRecordRef recordRefItem = new ListOrRecordRef();
recordRefItem.internalId = "898";
SelectCustomFieldRef scfr = new SelectCustomFieldRef();
scfr.scriptId = "custrecord_from_so_customer";
scfr.value = recordRefItem;//set the object value to the mentioned field
customFieldArray[1] = scfr;
rec.customFieldList = customFieldArray
Cannot find any documentation for this...
Currently using the following code to get a list of my photos:
FacebookApp fb = new FacebookApp(accessToken);
dynamic test = fb.Get("me/photos");
I'm cycling through the first 25 photos that it returns. Simple.
Now how do it get it to return the next 25?
So far I've tried this:
FacebookApp fb = new FacebookApp(accessToken);
string query = "me/photos";
while (true)
{
dynamic test = fb.Get(query);
foreach (dynamic each in test.data)
{
// do something here
}
query = test.paging.next;
}
but it fails throwing:
Could not parse '2010-08-30T17%3A58%3A56%2B0000' into a date or time.
Do I have to use a fresh dynamic variable for every request, or am I going about this the wrong way completely?
Ended up finding this:
// first set (1-25)
var parameters = new ExpandoObject();
parameters.limit = 25;
parameters.offset = 0;
app.Api("me/friends", parameters);
// next set (26-50)
var parameters = new ExpandoObject();
parameters.limit = 25;
parameters.offset = 25;
app.Api("me/friends", parameters);
I also found you can use this.
// for the first 25 albums (in this case) 1-25
dynamic albums = client.Get("me/albums", new { limit = "25", offset = "0"});
// for the next 25 albums, 26-50
dynamic albums = client.Get("me/albums", new { limit = "25", offset = "25"});
Worked the same as you used above.
hiya, i have the following code but when i try and create a new IQuerable i get an error that the interface cannot be implemented, if i take away the new i get a not implemented exception, have had to jump back and work on some old ASP classic sites for past month and for the life of me i can not wake my brain up into C# mode.
Could you please have a look at below and give me some clues on where i'm going wrong:
The code is to create a list of priceItems, but instead of a categoryID (int) i am going to be showing the name as string.
public ActionResult ViewPriceItems(int? page)
{
var crm = 0;
page = GetPage(page);
// try and create items2
IQueryable<ViewPriceItemsModel> items2 = new IQueryable<ViewPriceItemsModel>();
// the data to be paged,but unmodified
var olditems = PriceItem.All().OrderBy(x => x.PriceItemID);
foreach (var item in olditems)
{
// set category as the name not the ID for easier reading
items2.Concat(new [] {new ViewPriceItemsModel {ID = item.PriceItemID,
Name = item.PriceItem_Name,
Category = PriceCategory.SingleOrDefault(
x => x.PriceCategoryID == item.PriceItem_PriceCategory_ID).PriceCategory_Name,
Display = item.PriceItems_DisplayMethod}});
}
crm = olditems.Count() / MaxResultsPerPage;
ViewData["numtpages"] = crm;
ViewData["curtpage"] = page + 1;
// return a paged result set
return View(new PagedList<ViewPriceItemsModel>(items2, page ?? 0, MaxResultsPerPage));
}
many thanks
you do not need to create items2. remove the line with comment try and create items2. Use the following code. I have not tested this. But I hope this works.
var items2 = (from item in olditems
select new ViewPriceItemsModel
{
ID = item.PriceItemID,
Name = item.PriceItem_Name,
Category = PriceCategory.SingleOrDefault(
x => x.PriceCategoryID == item.PriceItem_PriceCategory_ID).PriceCategory_Name,
Display = item.PriceItems_DisplayMethod
}).AsQueryable();