ListItem not working with winforms [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I just simply trying to add items with values to a combobox from a XML file at runtime in a
windows application. The code below does not work in windows development.
//Code:
XDocument xDoc = XDocument.Load(#"Yourxmlfile.xml");
var query = from xEle in xDoc.Descendants("publication")
select new ListItem(xEle.Element("name").Value,
xEle.Attribute("tcmid").Value);
cmbLoad.ValueMember = "value";
cmbLoad.DisplayMember = "text";
cmbLoad.DataSource = query;
In the above code the ListItem class is not available for winforms so i couldn't proceed.
The above code works fine with web application.
Any help?

You can achieve that using this code:
XDocument xDoc = XDocument.Load(#"Yourxmlfile.xml");
var query = from xEle in xDoc.Descendants("publication")
select new { value = xEle.Element("name").Value, text = xEle.Attribute("tcmid").Value };
var list = query.ToList();
comboBox1.ValueMember = "value";
comboBox1.DisplayMember = "text";
comboBox1.DataSource = list;
ListItem doesn't exist for WinForms. I used an anonymous class above and converted it to a list which I can use as a source for a combo box.

Related

Assign a list of values to another list using linq in c# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to achieve the same code logic using Linq basically or re factoring the code would be great.
List<PublisherDto> listOfMappedPublishers = new List<PublisherDto>();
listOfPublishers.ForEach(pub =>
{
PublisherDto publisher = new PublisherDto();
publisher.ApiKey = pub.ApiKey;
publisher.Name = pub.Name;
publisher.Password = pub.Password;
listOfMappedPublishers.Add(publisher);
});
return listOfMappedPublishers;
You can use Select and ToList as follow:
List<PublisherDto> listOfMappedPublishers = listOfPublishers.Select(i=> new PublisherDto()
{
ApiKey = i.ApiKey,
Name = i.Name,
Password = i.Password
}).ToList();

Refactor code to make 3 size array dynamic in code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to refactor the code, I need to make this code dynamic, how to do that, see below part?
public class Trv34
{
public string CustomerNumber
public string ProductName
}
Models.Trv34[] itemTrv34 = new Models.Trv34[3]
itemTrv34[0] = new Models.Trv34 {CustomerNumber ="1", ProductName="p1"}
itemTrv34[1] = new Models.Trv34 {CustomerNumber ="2", ProductName="p2"}
itemTrv34[2] = new Models.Trv34 {CustomerNumber ="3", ProductName="p3"}
request.VODB.Trv34= new Models.Trv34[] {
itemTrv34[0],
itemTrv34[1],
itemTrv34[2],
};
I want to replace this part with something dynamic instead
itemTrv34[0],
itemTrv34[1],
itemTrv34[2],
The easiest way that comes to mind is to use Linq:
request.VODB.Trv34 = itemTrv34.ToArray();

C# initialize object [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The first 3 lines of code works fine..
How can I do the same when using object initializer ?
// works
Customer MyCustomerx = new Customer();
MyCustomerx.Location[0].place = "New York";
MyCustomerx.Location[1].place = "France";
// problem here
List<Customer> MyCustomer = new List<Customer>
{
new Customer() { Name= "Me",Location[0].place = "New York" }
}
There's no equivalent of that code within object initializers - you can't specify indexers like that. It's slightly unusual that it works even directly... I'd expect to have to add to a Locations property, rather than there being two already available which I could set an unconventionally-named property on. For example, this would be idiomatic:
Customer customer = new Customer {
Name = "Me",
Locations = {
new Location("New York"),
new Location("France")
}
};
(I'd probably put the name into a constructor parameter, mind you.)
You could then use that within a collection initializer, of course.

Move entire tree to its leftChild and assign new tree to rightChild and change the root's node [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I created a tree , but now (Inside the code) , I want to Move entire tree to its leftChild and assign a new tree to rightChild and change the root's node !
as shown in picture below:
In C# if it's possible.
From your image I guess you're making a binary tree. All a tree is when you get down to it is a series of nodes.
So assuming you have a node class like this.
public class MyBinaryNode
{
MyBinaryNode LeftChild;
MyBinaryNode RightChild;
MyBinaryNode()
{
LeftChild = null;
RightChild = null;
}
MyBinaryNode(MyBinaryNode leftChild, MyBinaryNode rightChild)
{
LeftChild = leftChild;
RightChild = rightChild;
}
}
All you'd have to do is something like this;
MyBinaryNode B = new MyBinaryNode();
MyBinaryNode C = new MyBinaryNode();
MyBinaryNode A = new MyBinaryNode(B,C); //This is your current tree
MyBinaryNode E = new MyBinaryNode();
MyBinaryNode D = new MyBinaryNode(A,E); //This is how you make the one in your Image

how to check whether dropdown list has all items in the given list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have got a requirement like I have got list of strings and I got dropdownlist items now we need to check whether the dropdownlist has all items in the give list of strings or not and i need to return the bool condition ...
for that purpose i have done like this ....
public static bool GetMacthedItems(DropDownList ddllist1, DropDownList ddllist2, string MatchedItem1, string MatchedItem2, string MatchedItem3)
{
bool results =true;
List<String> list1 = new List<String> {MatchedItem1, MatchedItem2, MatchedItem3};
if (ddllist1.SelectedValue.ToString() == MatchedItem1 && (list1.Any(x => x.Contains(ddllist2.SelectedValue.ToString()))))
{
results = false;
}
return results;
}
but the above condition is checking like whether the dropdownlist selected value is in that list or not ....
i want entire like all items in dropdownlist are in that list or not
would any one pls help on this ....
var allContained = ddlCountry.Items.Cast<ListItem>().Select(item => item.Value).All(item => lst.Contains(item.ToString());
Try this - for performance it will stop iterating once the condition is broken and return false by using a combination of All with Contains.

Categories