Use LINQ to populate controls from object array - c#

I am just trying to find a better way to populate some RadioButtonList controls. There are a set number of these on the usercontrol.ascx. The code is what I am currently using but I am still not very good with Linq and was wondering if there's a better way to do this.
quizId1 = quiz.Items[0].questionId;
pTag1.InnerText = quiz.Items[0].QuestionText;
foreach (Question q in quiz.Items[0].AnswerChoice)
{
radiobuttonlist1.Items.Add(new ListItem(q.Value, q.answerId));
}
same for radiobuttonlist2 but using Items[1] etc.
quizId2 = quiz.Items[1].questionId;
pTag2.InnerText = quiz.Items[1].QuestionText;
foreach (Question q in quiz.Items[1].AnswerChoice)
{
radiobuttonlist2.Items.Add(new ListItem(q.Value, q.answerId));
}
Sorry, the InnerText is a server side 'P' tag, pTag1, pTag2 etc.

Try this
var listItems = (from x in quiz.Items[0].AnswerChoice
select new ListItem { Text = q.Value, Value = q.answerId }
).ToList<ListItem>();
radiobuttonlist1.DataSource = listItems;
radiobuttonlist1.DataBind();

I suppose you could do something like this:
var listItems = from q in quiz.Items[0].AnswerChoice
select new ListItem(q.Value, q.answerId);
radioButtonlist1.Items.AddRange(listItems.ToArray());
though I'm not really sure if that buys you anything other than experience with LINQ...

I assume you want to use ling to popiulate several controls, not an individual control.
Linq is for querying, not updating. To update you still need either for or foreach loops.
If your RadioButtonList objects are in a collection, you could just loop over them:
var radioList = new [] {radiobuttonlist1, radiobuttonlist2, ...};
for(int i = 0; i < quiz.Items.Length; i++)
{
var quizId = quiz.Items[i].questionId;
radioList[i].InnerText = quiz.Items[i].QuestionText;
radioList[i].Items.AddRange(quiz.Items[i]
.AnswerChoice
.Select(q => => new ListItem(q.Value, q.answerId))
.ToArray()
);
}
or use DataSource as others have suggested:
var radioList = new [] {radiobuttonlist1, radiobuttonlist2, ...};
for(int i = 0; i < quiz.Items.Length; i++)
{
var quizId = quiz.Items[i].questionId;
radioList[i].InnerText = quiz.Items[i].QuestionText;
radioList[i].DataSource = quiz.Items[i].AnswerChoice;
radioList[i].DataTextField = "Value";
radioList[i].DataValueField = "answerId";
}
but there's no Linq way to populate several controls.

Related

Visual Basic C# with Selenium variable in css selector

I am only a week or so into C#, but thanks to forums like this I am able to piece together a bunch of code and get a general understanding. However, I have a problem with using a variable I can't solve.
List<IWebElement> elementList = new List<IWebElement>();
elementList.AddRange(browser.FindElements(By.CssSelector("div[class^='CalendarDays-calendarDays-']>div:nth-child(3)>div>div[class^='CalendarEvent-event-']")));
int t = elementList.Count;
for (int i = 1; i <= t; i++)
{
var item + i = browser.FindElement(By.CssSelector("div[class^='CalendarDays-calendarDays-']>div:nth-child(3)>div>div:nth-child(i)>div>div[class^='CalendarEvent-title']")).GetAttribute("textContent");
}
Problems are "var item + i" and "div:nth-child(i)". The first is automatically creating sequential variables and the second trying to get the nth-child using the variable "i". I can't figure out how to format "i" in either instance.
Any help would be appreciated.
Working Code after edits:
List<IWebElement> elementList = new List<IWebElement>();
elementList.AddRange(browser.FindElements(By.CssSelector("div[class^='CalendarDays-calendarDays-']>div:nth-child(3)>div>div[class^='CalendarEvent-event-']")));
int t = elementList.Count;
List<String> listItems = new List<String>();
for (int i = 1; i <= t; i++)
{
String item = browser.FindElement(By.CssSelector("div[class^='CalendarDays-calendarDays-']>div:nth-child(3)>div>div:nth-child(i)>div>div[class^='CalendarEvent-title']")).GetAttribute("textContent");
listItems.Add(item);
}
Thanks,
Don
You should use a list to store the different items, you have to know the type of GetAttribute. I assume it is String :
List<String> listItems = new List<String>();
for (int i = 1; i <= t; i++)
{
String item = browser.FindElement(By.CssSelector("div[class^='CalendarDays-calendarDays-']>div:nth-child(3)>div>div:nth-child(i)>div>div[class^='CalendarEvent-title']")).GetAttribute("textContent");
listItems.Add(item);
}
then you can access the list items by index listItems[i]
For the second part of the question you have to concatenate the string with the i of the for loop like this:
browser.FindElement(By.CssSelector("div[class^='CalendarDays-calendarDays-']>div:nth-child(3)>div>div:nth-child("+i+")>div>div[class^='CalendarEvent-title']")).GetAttribute("textContent");
You can't do var item + i. Just use the same variable each time, it's inside the loop scope anyway.
To insert the i variable to the locator you can use string interpolation
var item = browser.FindElement(By.CssSelector($"div[class^='CalendarDays-calendarDays-']>div:nth-child(3)>div>div:nth-child({i})>div>div[class^='CalendarEvent-title']")).GetAttribute("textContent");

Compare list data with static string in C#

I have the following list,
String UpDownStatus = "UP";
List<db1> StockData = new List<db1>();
db1 newStock = new db1();
newStock.Date = (DateTime)reader["Date"];
newStock.High = (double)reader["High"];
newStock.Low = (double)reader["Low"];
newStock.Close = (double)reader["Close"];
newStock.Up_Down = (string)reader["Up_Down"];
StockData.Add(newStock);
The Up_Down column in my DB has the following two string : "UP" and "DOWN"
how can i compare whether the current value of
for (int i = 0; i < StockData.Count; i++)
{
if(StockData[i].Up_Down.CompareTo(UpDownStatus) != 0)
{
//do something
}
}
I know, i could reframe this statement like if (StockData[i].Up_Down=="UP") but i need a solution where I could use .CompareTo function.
If I understood your question right and you want to do something on all items which have the same up/down-status as the variable UpDownStatus, this would be the easiest way to do it:
foreach (var item in StockData.Where(e => e.Up_Down == UpDownStatus))
{
// do something
}

Filtering a List with an array items

i need to filter a list with strings in an array. Below code doesn't return the expected result.
List<searchModel> obj=//datasource is assigned from database
mystring="city1,city2";
string[] subs = mystring.Split(',');
foreach (string item in subs)
{
obj = obj.Where(o => o.property.city.ToLower().Contains(item)).ToList();
}
As far as you're using Contains, I'd say you could be trying to get
entries, city of which matches any city from mystring
entries, city of which match all cities from mystring
So, having (I simplified searchModel class, having omitted property):
List<searchModel> obj = new List<searchModel>
{
new searchModel{city = "city1"},
new searchModel{city = "city2"},
new searchModel{city = "city3"}
};
var mystring = "city1,city2";
var subs = mystring.Split(',').ToList(); //let it be also List<T>
We can do:
//the 1st option
var orFilter = obj.Where(o => subs.Any(s => o.city.ToLower().Contains(s)))
.ToList();
//the 2nd option
var andFilter = obj.Where(o => subs.TrueForAll(s => o.city.ToLower().Contains(s)))
.ToList();
Then do a simple check
foreach (var o in andFilter)
{
Console.WriteLine(o.city);
}
I'd say that the OP is equal to option 2, not option 1.
I think you want this, or something close - I haven't tested it:
List<searchModel> obj=//datasource is assigned from database
mystring="city1,city2";
string[] subs = mystring.Split(',');
obj = obj.Where(o => subs.Contains(o.property.city.ToLower())).ToList();
What you're currently doing is filtering the list by ALL cities. So you'll only return results where o.property.city equals "city1" and "city2" (and any other cities you might have in the list). So you won't get any results.
To match any city in the list instead, try this:
var myFilteredObj = obj.Where(o => subs.Contains(o.property.city.ToLower()).ToList();
I add this codes of lines, probably will help someone and maybe someone will optimize it:
var jaggedArray = new string[100][];
var i = 0;
jaggedArray[i] = {"first folder","first file","first 5 files","last 5 folder"};
filter = "irs le";
var arrFilters = filter.Split(' ');
foreach (var arrFilter in arrFilters)
{
jaggedArray[i] = jaggedArray[i].Where(p =>p.ToLower().Contains(arrFilter.ToLower())).ToArray();
jaggedArray[i + 1] = jaggedArray[i];
i++;
}
return jaggedArray[i]; //new array after filter
//result: "first file","first 5 files"
var result = obj.Where(piD => subs.Contains(piD.city)).ToList();
The code above will filter the obj List based on the string array.

Foreach and modification to iteration variable

What's the proper way to do this?
foreach(Object obj in ObjectList<Object>)
{
Object changedObject= GetInfo(obj);
obj=changedObject; //no good
obj.prop1 = changedObject.prop1; //ok?
obj.prop2 = changedObject.prop2; //ok?
better way?
}
Use a for loop instead:
for (int i = 0; i < list.Count; i++)
{
Object changedObject = GetInfo(list[i]);
list[i] = changedObject;
}
You could also use LINQ, and append .ToList() if you need a list instead of an IEnumerable<T>:
var query = result.Select(o => GetInfo(o));
I think you're better off creating a new list of objects, ie:
var newList = new List<Object>();
foreach(Object obj in ObjectList<Object>)
{
newList.Add(GetInfo(obj));
}
Then either replace ObjectList with newList or just return it from whatever you're doing

LINQ adding to list object in loop

I have a situation where I need to populate a list object by looping through a loop of objects, check against a condition, and if the condition is met, add the item to my list. Currently I cannot find an example of the syntax required to add to the list rather than clear it and repopulate it each time the loop iterates to the next item. Can somebody help?
List<ProfileRightsJSON> prf = new List<ProfileRightsJSON>();
try
{
for (int i = 0; i < lstProfiles.Count; i++)
{
prf = (from p in _database.tblProfileRights
where p.fkProfileID ==
lstProfiles[i].ProfileID
select new ProfileRightsJSON
{
FunctionID = p.fkFunctionID,
UserTypeID = p.fkUserTypeID,
RecursiveRights = p.RecursiveRights
}).ToList();
}
It looks like you really want something like this:
var profileIds = lstProfiles.Select(x => x.ProfileID).ToList();
var prf = (from p in _database.tblProfileRights
where profileIds.Contains(p.fkProfileID)
select new ProfileRightsJSON
{
FunctionID = p.fkFunctionID,
UserTypeID = p.fkUserTypeID,
RecursiveRights = p.RecursiveRights
}).ToList();

Categories