So I have a two columns from a database that will return all the products that are in my store and the department id's that are associated with the product.
What I want to do is create something using a list/dictionary/ienumerable set so that if I give a function a product id, it will spit out the department id. At the moment I am having some troubles getting the declarations correct and need some help in that department.
First I have the base for the relationship between the Product and Category. Then I want ProductCategoryCollection to return the collection of all the mappings for each product and category/department. I am stuck in the second part and not really sure where to go from where I am at.
helper.GetProductToCategoryMatching() returns the rows from the database.
public class ProductAndCategoryID
{
public ProductAndCategoryID(int product, int category)
{
this.productID = product;
this.categoryID = category;
}
public int productID;
public int categoryID;
}
public class ProductCategoryCollection : IEnumerable<ProductAndCategoryID>
{
public ProductCategoryCollection()
{
}
public List<ProductCategoryCollection> populate()
{
ShippingClassHelper helper = new ShippingClassHelper();
DataSet ds = new DataSet();
List<ProductCategoryCollection> list = new List<ProductCategoryCollection>();
ds = helper.GetProductToCategoryMatching();
foreach (DataRow row in ds.Tables[0].Rows)
{
}
return new List<ProductCategoryCollection>();
}
}
All you need to do now is create a ProductCategoryCollection object inside of your loop and add it to your list.
public List<ProductAndCategoryID> populate()
{
ShippingClassHelper helper = new ShippingClassHelper();
DataSet ds = new DataSet();
List<ProductAndCategoryID> list = new List<ProductAndCategoryID>();
ds = helper.GetProductToCategoryMatching();
foreach (DataRow row in ds.Tables[0].Rows)
{
var pc = new ProductAndCategoryID();
pc.ProductID = row[0];
pc.CategoryID = row[1];
list.Add(pc);
}
return list;
}
If I understand your question and your requirements correctly, you would like to get a dictionary that maps a ProductID to a CategoryID, so that a lookup can be performed for the CategoryID of a given ProductID.
If that is a good translation of your problem, this is what you can do:
var productMap = new ShippingClassHelper()
.GetProductToCategoryMatching()
.Tables[0].Rows
.ToDictionary(row => (int)row[0], row => (int)row[1]);
It makes the following assumptions:
The "ProductID" field is an integer and the first field in a row.
The "CategoryID" field is an integer and the second field in a row.
Your dataset does not contain duplicate "ProductID" values.
Now you can use this dictionary to perform lookups. If you want to check if a given product id exists, you can do:
var containsProduct660 = productMap.ContainsKey(660);
If you want to retrieve the category id for a given product id you can do:
var categoryIdForProduct660 = productMap[660];
Related
I am have a Windows Forms application and also using SQL to get the data from my db. I have a binded checkListBox in my Windows Forms application and I would like to get the primary key (the value) of all the checked Items in my checkListBox. What I realize is in combo box, they have a build in method 'selectedValue' I am wondering if we can do the same thing in checkListBox by looping through the checkedItems in checkListBox and get its value
Let say in my db table, I have:
EmployeeId Employee Name
1 Jimmy
2 Pam
3 Bob
4 Kobe
My binded checkListBox showed something like
[x]Jimmy
[x]Pam
[]Bob
[]Kobe
Can I get the checkedItem item's id by using sql query. Now let say Jimmy and Pam is checked and I want to get their employeeId. I am not sure how to do it. Help will be appreciated.
select * from employee_table where employeeId = '"+checkListBox.SelectedValue+"'
or
foreach(var item = item.CheckedItems{
select * from employee_table where employeeId = '"items"';
}
You don't need to go to database again to retrieve Id of your items.
You can create a class for items that contains an Id and a Name property and override ToString method to return the string that you want to show in CheckedListBox:
public class ItemModel
{
public int Id { get; set; }
public string Name { get; set; }
public override string ToString()
{
return Name;
}
}
Then when loading data, you can select the output and shape it to ItemModel and then add these ItemModels to CheckedListBox:
private void Form_Load(object sender, EventArgs e)
{
var db = new TestDBEntities();
//Select items that you need and shape it to ItemModel
var list = db.Categories.Select(x => new ItemModel
{
Id = x.Id,
Name = x.Name
})
.ToList();
//We cast the list to object[] because AddRange method accept object[]
this.checkedListBox1.Items.AddRange(list.Cast<object>().ToArray());
}
Then when you need to know Id of checked items, you can simply cast each checked item to ItemModel an use its Id property:
private void button1_Click(object sender, EventArgs e)
{
this.checkedListBox1.CheckedItems.Cast<ItemModel>()
.ToList()
.ForEach(item =>
{
MessageBox.Show(string.Format("Id:{0}, Name:{1}", item.Id, item.Name));
});
}
Note:
If you use another method to connect to database, you can simply change this code to satisfy your requirements, for example this code uses ADO.Net objects to shape data to ItemModel:
private void CheckedListBoxSample_Load(object sender, EventArgs e)
{
var connection = #"data source=(localdb)\v11.0;initial catalog=TestDB;integrated security=True;MultipleActiveResultSets=True;";
var command = "SELECT Id, Name From Categories";
var dataAdapter = new System.Data.SqlClient.SqlDataAdapter(command, connection);
var table = new DataTable();
dataAdapter.Fill(table);
var list = table.Rows.Cast<DataRow>()
.Select(row => new ItemModel
{
Id = row.Field<int>("Id"),
Name = row.Field<string>("Name")
})
.ToList();
this.checkedListBox1.Items.AddRange(list.Cast<object>().ToArray());
}
Something along these lines might help you.
Basically what it does, is that it builds a parameterized query, and then adds all the selected items using SqlParameters. Then using the reader, you are able to parse each returned record. The code below will probably need some modifications for you to use it, but should help you getting started.
Although I would strongly suggest using an ORM like Entity Framework. Using an ORM you don't have to build your own queries, and allows you to use strong typed classes to "talk" to your database.
var query = "SELECT * FROM employee_table WHERE imployeeId IN (#ids)";
var ids = String.Join(","
MyCheckBoxList.Items
.Cast<ListItem>()
.Where(x => x.Selected)
.Select(x => x.Value);
using (var connection = new SqlConnection(myConnectionString))
{
connection.Open();
using(var command = new SqlCommand(query, connection)
{
command.Parameters.Add("ids", ids);
var reader = command.ExecuteReader();
while(reader.Read())
{
//get all the needed data from the reader
}
}
}
I am creating a Pie Chart and its name-value pair are being retrieved from the database.
how to read the row details in order to get the values and store it one property ?
public override void OnApplyTemplate()
{
Ram.DataContext = new List<UsageRAM>()
{ new UsageRAM() { name = "Available" , value =/*how to get the value here*/ },
new UsageRAM() { name = "Used" , value =/*how to get the value here*/ }};
base.OnApplyTemplate();
}
public class UsageRAM
{
public string name { get; set; }
public double value { get; set; }
}
EDIT
--Answer which worked for me--
using (DB db = new DB())
{
var row = (from c in db.UsageRAMs
orderby c.UsageRamID descending
select c).First();
Ram.DataContext = new List<UsageRAM>()
{ new UsageRAM() { name = "Available" , value = row.Available},
new UsageRAM() { name = "Used" , value = row.Used }};
If you're using EF, simply add a new model to your project and include the requried table(s) in this model. Then you can use the newly created Entities object to read your db values as follows:
var Rows = context.YourTableName.Where(YourCondition).ToArray();
You can then iterate over the Rows collection using foreach or something.
If you need to read values from a single row, you may want to use First instead of Where above. That will return a single object whose properties will map to your db fields and can directly be assigned in the example code you posted in the question.
EXAMPLE
Say your model's Entity class is named MyEntities and it contains the table UsageRam, which has two fields Available and Used. Then it will take the following code:
using(MyEntities e = new MyEntities)
{
var Row = e.MyTable.First(x => x.UsageRamID = **[ID]**);
MessageBox.Show("Available=" + Row.Available.ToString() + ",Used=" + Row.Used.ToString();
}
I have just shown values in message box, you can assign them to anything you want.
Another Example
using(MyEntities e = new MyEntities)
{
var Rows = e.MyTable.Where(x => x.UsageRamID > 10 && x.UsageRamID < 20);
foreach(var Row in Rows)
MessageBox.Show("Available=" + Row.Available.ToString() + ",Used=" + Row.Used.ToString();
}
EXAMPLE 3
The code in your updated post appears fine to me, though I do have some doubts about the db design, but given your table is indexed on RamUsageID column, this should give you correct results without much performance impact. I generally prefer lambada expressions over query, so I'd rather write it like:
db.RamUsage.OrderByDescending(x => x.RamUsageID).First()
I have a little task, I want assign data to my combobox as datasource, display member, value member from SQL Server table of a single column, by using Linq-to-SQL.
I get the required data from table like
var Uom = SpOpDcontx.Ptr_UOMs.Select(c => c.UM_Typ).Distinct().ToArray();
The data is being assigned to Uom variable, so I want to assign this data into my combobox datasource. Can any one help me please?
I don't understand why you mention a datatable when you don't use it and don't need it for this anyway. You simply need to do the following:
combo.DataSource = Uom;
combo.DisplayMember="UM_Typ";
combo.ValueMember="UM_Typ";
why don't you use List?
public class Test
{
// your all return properties goes here
public string Typ;
}
public class Test2
{
public List<Test> GetRequireData()
{
var Uom = SpOpDcontx.Ptr_UOMs.Select(c => new Test()
{
Typ = c.UM_Typ)
})
.Distinct();
return Uom.ToList<Test>();
}
}
Now, you can call GetRequireData() in your form as;
var obj = new Test2();
combo.DisplayMember="UM_Typ";
combo.ValueMember="UM_Typ";
combo.DataSource = obj.GetRequireData();
I've been struggling with this for days, and I'm getting DataTables and dataGridViews all mixed up.
I have a WinForms program, which has a DataGridView, dataGridView1, and a DataTable, errors.
public static DataTable errors = new DataTable();
dataGridView1.DataSource = errors;
Now, further down, in a method called ValidateText, I read data from a text file, line by line, into an array, where I also define the columns for the errors datatable:
errors.Columns.Add("Account Number");
errors.Columns.Add("Customer Name");
errors.Columns.Add("Country");
errors.Columns.Add("State");
errors.Columns.Add("Ship-to Country");
errors.Columns.Add("Ship-to State");
var lines = File.ReadAllLines(file);
foreach (string line in lines)
{.
.
.
string []items=line.Split('\t').ToArray();
errors.Rows.Add(items[0], items[1],...items[5]);
And that works just fine when I run it. the thing is, I want to make the "Country" column have a combo box in it, so when the program is run, and the data is displayed in the dataGridView1, the user will have the opportunity to select a new country, if they want, from the "Countries" column. And further down in the program, I've indeed defined a
method that creates a DataGridViewComboBoxColumn
private DataGridViewComboBoxColumn CreateComboBoxColumn()
{
DataGridViewComboBoxColumn buildCountries = new DataGridViewComboBoxColumn();
buildCountries.HeaderText = "List of Countries";
DataTable dataTable = new DataTable();
dataTable.Columns.Add("Keys");
dataTable.Columns.Add("Values");
KeyValuePair<string, string>[] array = CountryList.ToArray();
foreach (KeyValuePair<string, string> kvp in array)
{
dataTable.Rows.Add(kvp.Key, kvp.Value);
}
buildCountries.DataSource = dataTable;
buildCountries.DisplayMember = "Values";
buildCountries.ValueMember = "Keys";
return buildCountries;
}
The thing that I am having problems with, is how do I get that combo box that I've created in that last method, into the "Countries" column I've created above? I feel like I'm not getting something with DataGridView and DataTable. You bind a DataTable to a DataGridView, but adding this DataGridViewComboBoxColumn seems to be hard.
Thanks,
Amanda
this is just my suggestion, the .NET Framework had a already list of countries. so you can use this class. i forgot who created this code :)
public static class CountryEntries
{
public static IEnumerable<Country> GetCountries()
{
return from ri in
from ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures)
select new RegionInfo(ci.LCID)
orderby ri.DisplayName
group ri by ri.TwoLetterISORegionName into g
select new Country
{
CountryId = g.Key,
Title = g.First().DisplayName
};
}
public class Country
{
public string CountryId { get; set; }
public string Title { get; set; }
}
}
i am currently working with SQL CE & WPF . in the middle of coding i struck with no idea of converting the dataset of a Database to my observablecollection which is binds to the UI Controllers like Listbox and listview.
plz guide me , if possible with code !!
Let's say your DataSet contains a Table named Person and that Person has columns Id, Name, and Age.
You first create a Person class to hold your Person data:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
Then you use LINQ to populate your ObservableCollection:
var people = new ObservableCollection<Person>(
dataset.Tables["Person"].AsEnumerable().Select(p => new Person
{
Id = p.Field<int>("Id"),
Name = p.Field<string>("Name"),
Age = p.Field<int>("Age")
}));
You will need to add the following assembly reference to your project in order to use the AsEnumerable<T>() and Field<T>() extension methods:
System.Data.DataSetExtensions (in System.Data.DataSetExtensions.dll)
Edit
In response to your comment, you would normally process each change you make to the data immediately rather than try to convert the whole ObservableCollection back to the DataSet. So, for example, if you add a new Person to your ObservableCollection, you would also want to add that Person to the DataSet.
Add a person:
var table = dataSet.Tables["Person"];
var row = table.NewRow();
row["Id"] = person.Id;
row["Name"] = person.Name;
row["Age"] = person.Age;
table.Rows.Add(row);
Delete a person:
var table = dataSet.Tables["Person"];
var row = table.AsEnumerable().Where(p => p.Id == person.Id);
row.Delete();
Update a person (say you want to change the age to 37):
var table = dataSet.Tables["Person"];
var row = table.AsEnumerable().Where(p => p.Id == person.Id);
var row["Age"] = 37;
You might also want to look into LINQ to SQL because it automates a lot of this stuff.