I have a collection:
App.ViewModel.historyItemCollection
That has 4 properties:
id
name
meterValue
meterDate
I would like to bind to my listbox by id. So if I select house #2 (in the collection) I only want to show the history information for house 2.
I tried a couple of different selects/where statements when trying to bind it to my form:
lbHistory.ItemsSource = App.ViewModel.historyItemCollection.Where(history => history.id= houseIndex);
If there are any links that say how to do this, please just point me there? Part of my problem I couldn't figure out what this was called so my searches were fruitless.
Thanks!
I haven't tested this code, so forgive me if it doesn't work.
lbHistory.ItemsSource = from item in App.ViewModel.historyItemCollection
where item.id == houseIndex
select item;
If there can be more than one match, and you only want to show the first, use:
lbHistory.ItemsSource = ( from item in App.ViewModel.historyItemCollection
where item.id == houseIndex
select item ).FirstOrDefault();
Related
I have a database which it's schema is as follows:
As you can see, We have WareCategories which will be category of the wares i'm going to be working in my website. WareTypes which will be Definition of each Item type. Categories define properties in the table WarePropertyDefinitions and WareProperties define values for each property that has been defined in WarePropertyDefinitions table.
Now i have a search page that users search for items in OldWares and user selects category and i show the user all properties defined in WarePropertyDefinitions and user fills the data if he likes better results. But my problem is that i can't filter WareTypes based on WareProperties because it's from the type ICollection and i can't access filter options.
How can i apply this kind of filtering based on properties?
Thanks in advance...
Edit:
This is a part of the code i'm presenting to describe more:
var lst = WareCategory.GetItem(Convert.ToInt32(ddlChildren.SelectedValue)).WarePropertyDefinitions.ToList();
foreach (var ListItem in lst)
{
var value = BaseHelper.FindFormValue("PropertyValue" + ListItem.Id.ToString());
if (!string.IsNullOrEmpty(value))
{
query = query.Where(m => m.WareType.WareProperties.);
}
}
}
This segment of code is in my search function and as you can see i'm going to generate a list of items in WarePropertyDefinition that user selected via a drop down menu called ddlChildren. I'm going to iterate in this definition and user entered value for each property (the value variable will hold the value user entered and i will check if user have entered anything in the textbox) i will include it in where section (through this i will add it in where clause that ultimately filters my selection). but as you can see the code is incomplete because i don't know how to complete it.
Use the Any() extension method, for example:
query = query.Where(m => m.WareType.WareProperties.Any(wp => wp.Id == 5));
I have fixed the my problem by this code:
query = query.Where(m => m.WareType.WareProperties.Any(wp => wp.WarePropertyDefinition_Id == ListItem.Id && wp.TextValue == value));
but because #user3159792's answer was the basic of my problem i have selected his answer as the default answer to my problem. very thanks.
I have two List T objects. One is a list of forums available and the other the forums the user has selected. This is executing during a jquery ajax call and will be populating a listbox.
The purpose is that I want to flag the forums the user has subscribed to with "selected" which will of course render in the html listbox as a highlighted row.
I started to write this query and stopped short.
var result = (from exf in ExtForum
join custfrm in customer.ExternalForums on
exf.Id equals custfrm.Id
select new { id=exf.Id, name=exf.ForumName, isSelected=(true ? "selected" : "") })
.ToList();
This will only return the forums that match. What I need is a left outer query but in thinking about that I don't think that is correct either. Well it is sort of correct as it would return all available forums but now I am back to square one of figuring out how I would flag my matches.
My approach which is open to improvement is to return basically a string object to jquery that then updates the html control.
ddlExtBoards.append($('<option></option>').val(option.id).html(option.name + " " + option.isSelected));
So advice / direction on what approach to accomplish this "match" would be very much appreciated.
The purpose is that I want to flag the forums the user has subscribed to with "selected"
A left-join is what you want here, this means that all the records in ExtForum are included at least once and any other ones that match the join
var results = (from exf in ExtForum
join custfrm in customer.ExternalForums on
exf.Id equals custfrm.Id into customerForums
from custForum in customerForums.DefaultIfEmpty()
select new
{
id = exf.Id,
name = exf.ForumName,
isSelected = custForum != null
}
I have a WPF auto complete box filled with List which have repetitive items.
Now when an item is selected i need to get the index so that i want to identify uniquely which item is selected. I cannot go by text in auto complete box alone because they are repetitive and so that need to go in conjunction with selected index.
Seems simple enough but i have not been able to find a solution for this. I see there is a
autocompletebox.selecteditem
I have not been able to get anything out of that for scenario mentioned. Please suggest.
There is no index for selected item for auto complete box.
So the solution would be to manage that in your code logic.
This is what i did when i faced an issue where i have to select the first available item when the auto complete box displayed multiple products and user wants to search for a product that is available.
The first scenario would be to display the list of all products and let him search and select a product. So here the user would be entering the product name (which might be repetitive in the auto complete box)
So you have a search on the List of all products. But now when searching you first loop through your list and find if you are searching for a product name that occurs multiple times.
foreach (var products in ListofAllProducts)
{
if (Products.ProductName.ToString() == productname)
{
i=i+1;
tempproducts.Add(products );
}
}
Now you find the first available item by checking the customer name
foreach (var product in ListofAllProducts)
{
if (i == 1)
{
if (Product.ProductName== productname)
{
return product;
}
}
else
{
foreach (var product in ListofAllProducts)
{
if (int.Parse(product .CustomerID)==0)
{
return product ;
}
}
return tempproducts[0];
}
}
return null;
}
Hi there I have searched for a while now and can't seem to find a solution to my problem, I have tried multiple methods to select multiple items in my listbox through code however none have worked, The best result I got was 1 selected item in my listbox.
Basically I want to select multiple items of the same value.
below is my code, sorry if I seem newbie but I am new to programming and still learning basic stuff.
foreach (string p in listBox1.Items)
{
if (p == searchstring)
{
index = listBox1.Items.IndexOf(p);
listBox1.SetSelected(index,true);
}
}
So as you can see I am trying to tell the program to loop through all the items in my listbox, and for every item that equals "searchstring" get the index and set it as selected.
However all this code does is select the first item in the list that equals "searchstring" makes it selected and stops, it doesn't iterate through all the "searchstring" items.
As suggested in the comment, you should set SelectionMode to either MulitSimple or MultiExpanded depending on your needs, but you also need to use for or while loop instead offoreach, because foreach loop doesn't allow the collection to be changed during iterations. Therefore, even setting this Property won't make your code run and you will get the exception. Try this:
for(int i = 0; i<listBox1.Items.Count;i++)
{
string p = listBox1.Items[i].ToString();
if (p == searchstring)
{
listBox1.SetSelected(i, true);
}
}
You can set SelectionMode either in the Properties window when using designer or in, for instance, constructor of your Form using this code:
listBox1.SelectionMode = System.Windows.Forms.SelectionMode.MultiSimple;
How far is the code for best functionallity?
I have two ComboBox, so the first is related for choose the company, and the second for choose the branch-office in relation with the one.
I note that the only way I can fill datasource with filtering .Where on LINQ is on this way, maybe Im wrong please take a moment for look the following snippet :
private void cboCompany_SelectedIndexChanged(object sender, EventArgs e)
{
var _index = ((ComboBox)sender).SelectedIndex;
using (DB db = new DB())
{
var su = (from s in db.Branchs select s);
if (cboCompany.SelectedIndex == 0)
{
cboBranch.DataSource = su.Where(x => x.codeCompany == 1).Select(x => x.name).ToList();
}
else if (cboCompany.SelectedIndex == 1)
{
cboBranch.DataSource = su.Where(x => x.codeCompany == 2).Select(x => x.name).ToList();
}
cboBranch.BindingContext = this.BindingContext;
cboBranch.DisplayMember = "name";
cboBranch.SelectedIndex = 0;
}
}
Thanks in Advance!
Rather than hand-coding this, I would make data binding do all this work for me. In particular, it can be set up thus:
Make it so that your Company class has a property to get all associated branches - e.g. Company.Branches. If you use LINQ to SQL or Entity Framework, there should be one there already.
Have two BindingSources, bsCompanies and bsBranches.
Set cboCompany.DataSource to bsCompanies, and cboBranch.DataSource to bsBranches
Set bsCompanies.DataSource to collection/DataSet that contains companies.
Set bsBranches.DataSource to Branches under bsCompanies (the form designer should let you do this after you do the previous step, if your collection is strongly typed).
Now whenever user picks a different company in the first combo, the current item in the companies binding source will change. This will cause binding for the second binding source to re-evaluate, and set list of branches for a newly selected company to be the source for the second combo.