In my page i need to add 10,20,30,40,50 etc to dropdown.For that in my controller i added like this.
var resultsPerPage = new Collection<SelectListItem>(){
new SelectListItem { Text = "10", Value = "10" },
new SelectListItem { Text = "20", Value = "20" },
new SelectListItem { Text = "30", Value = "30" },
new SelectListItem { Text = "40", Value = "40" },
new SelectListItem { Text = "50", Value = "50" }};
But in code violation showing don't use multiple statements like that.can you give me any suggestion to avoid this lines of code.
following your pattern may be something like this may fit your needs:
var data = new List<int> {19, 29, 39, 49 ,50}; //COLLECTION OF ALL POSSIBLE VALUES
var resultsPerPage = new Collection<SelectListItem>(); //COLLECTION OF ITEMS
//INIT COLLECTION OF ITEMS
foreach(var v in data) {
resultsPerPage.Add(
new SelectListItem { Text = v .ToString(), Value = v .ToString() }
);
}
Related
I'm extremely new to coding, so the answer/s to this may be obvious. I have to make the card game War. I've created a list of strings like so for a part of the deck:
List<string> userDeck = new List<string>
{
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"Jack",
"Queen",
"King",
"Ace",
};
Is there a way I can instruct the computer that "Jack" will be greater than "10", "Queen" greater than "Jack", and so on? I'm not sure where or how I should do it.
OR, if you have any other suggestions as to how I should do this, please let me know. I have to be using a list. I initially made a list of integers instead, but I wasn't aware of an uncomplicated way to assign the names such as "Jack," "Queen," "King," etc. to them for display purposes.
Any help would be greatly appreciated.
Try to create a object called "Card". This object can contain more than a value. Eg:
public class MyCard
{
public string Name {get;set;}
public int Value {get;set;}
public MyCard(string name, int value)
{
this.Name = name;
this.Value = value;
}
}
After create this object, you will use this at your list.
List<MyCard> userDeck = new List<MyCard>();
You can fill the list this way:
usertDeck.Add(new MyCard("2", 1));
....
usertDeck.Add(new MyCard("K", 11));
So, to compare 2 cards, just check using the "Value" variable
The easiest way to refactor your code (to your requirements value per card) is to use Tuple's instead of string type in generic List as follow:
List<Tuple<int,string>> userDeck = new List<Tuple<int,string>>
{
new Tuple<int,string>(2,"2"),
new Tuple<int,string>(3,"3"),
new Tuple<int,string>(4,"4"),
new Tuple<int,string>(5,"5"),
new Tuple<int,string>(6,"6"),
new Tuple<int,string>(7,"7"),
new Tuple<int,string>(8,"8"),
new Tuple<int,string>(9,"9"),
new Tuple<int,string>(10,"10"),
new Tuple<int,string>(11,"Jack"),
new Tuple<int,string>(12,"Queen"),
new Tuple<int,string>(13,"King"),
new Tuple<int,string>(14,"Ace"),
};
Similar to Vincius's answer but with some changes for better usability:
enum Suit
{
Clubs = 1,
Diamonds = 2,
Hearts = 3,
Spades = 4
}
class Card
{
private static readonly Dictionary<string, int> rankMap = new Dictionary<string, int>()
{
{"2", 2 },
{"3", 3 },
{"4", 4 },
{"5", 5 },
{"6", 6 },
{"7", 7 },
{"8", 8 },
{"9", 9 },
{"10", 10 },
{"Jack", 11 },
{"Queen", 12 },
{"King", 13 },
{"Ace", 14 },
};
private Suit suit;
private string rank;
public Suit Suit => suit;
public string Rank => rank;
public int Value { get { return rankMap[rank]; } }
public Card(Suit s, string r)
{
suit = s;
rank = r;
}
}
The way you can use it:
Card c1 = new Card(1, "Jack"); // 1 is Clubs
Card c2 = new Card(4, "Queen"); // 4 is Spades
Console.WriteLine(c1.Value); // prints 11
Console.WriteLine(c2.Value); // prints 12
How do I create a method setOutput() that calculates and sets the max output of each item (a,b,c,d) in Formula by multiplying the percentage in Formula to the amt in Item through its common id? I think I worked out the logic but I don't know how to use the id to reference values for calculation.
I.e. how do I reference values by their id within arrays in a nested foreach loop?
Class Item {
public string id { get;set }
public double? amt { get;set } }
Class Formula {
public string id { get;set }
public double? percent { get;set }
public double? output { get;set } }
Item[] inventory = {
new Item { id = "a", amt = 111.1},
new Item { id = "b", amt = 222.2},
new Item { id = "c", amt = 333.3,
new Item { id = "d", amt = 400.4} }
Formula[] formulas = { new Formula {
{id = "a", percent=25.0, output = null},
{id = "b", percent=25.0, output = null},
{id = "c", percent=25.0, output = null},
{id = "d", percent=25.0, output = null}; },
new Formula {
{id = "a", percent=20.0, output = null};
{id = "b", percent=20.0, output = null};
{id = "c", percent=60.0, output = null};
{id = "d", percent= 0.0, output = null}; },
new Formula {
{id = "a", percent=30.0, output = null};
{id = "b", percent=30.0, output = null};
{id = "c", percent=20.0, output = null};
{id = "d", percent=20.0} output = null}; } }
setOutput(Item[] inventory, Formula[] formulas)
{
// Loop through each Formula in formulas
// Loop through each Item in
// Item.amt * Formula.percentage/100 = Formula.amt
foreach (var Formula in formulas) //
{
foreach (Item item in Raw)
{
(id="a") Raw.amt* Formula.percent/100 = Formula.output
(id="b") Raw.amt* Formula.percent/100 = Formula.output
(id="c") Raw.amt* Formula.percent/100 = Formula.output
(id="d") Raw.amt* Formula.percent/100 = Formula.output
}
}
}
Hi assuming if your Item array has Items with unique id's something like below in your code
Item[] inventory = {new Item { id = "a", amt = 111.1},
new Item { id = "b", amt = 222.2},
new Item { id = "c", amt = 333.3 },
new Item { id = "d", amt = 400.4} };
Formula[] formulas = { new Formula {id = "a", percent=25.0, output = null} ,
new Formula {id = "b", percent=25.0, output = null} ,
new Formula {id = "c", percent=25.0, output = null},
new Formula {id = "d", percent=25.0, output = null} };
Then in your setouput function you could do something like below to set your output property in formulas array
formulas.ToList().ForEach(var => var.output = var.percent * inventory.First(var1 => var1.id == var.id).amt / 100);
A better approach is to make formulas data type from Formula[] to List
Assuming you meant for your formulas to be List<Formula>[] so you could have lists of Formulas, converting the inventory of Items to a Dictionary makes it easy:
void setOutput(Item[] inventory, List<Formula>[] formulas) {
var Raw = inventory.ToDictionary(i => i.id, i => i.amt);
foreach (var FormulaSet in formulas)
foreach (var formula in FormulaSet)
formula.output = Raw[formula.id] * formula.percent / 100;
}
I have data table having rows like
ID Name
2 A
4 B
3 C
5 D
1 E
List order = new List() { "1", "3", "2", "5", "4" }
--------------order by list-----------------
ID Name
1 E
3 C
2 A
5 D
4 B
can anyone help to implement this.. I am using DataTable in Winforms.
Another solution to the already given ones, would be to loop your order list and then sort your source list.
// source list
List<Foo> lSource = new List<Foo>() {
new Foo() { ID = 2, Name = "A" },
new Foo() { ID = 4, Name = "B" },
new Foo() { ID = 3, Name = "C" },
new Foo() { ID = 5, Name = "D" },
new Foo() { ID = 1, Name = "E" },
};
// order list
List<int> order = new List<int>() { 1, 3, 2, 5, 4 };
// loop order list and sort source list
order.ForEach(x =>
{
lSource = lSource.OrderBy(g => g.ID == x).ToList();
});
// set datasource
dataGridView1.DataSource = lSource;
I just added a class Foo containing an int ID and a string Name, because you didn't share your whole code.
I think you can join your order and your datatable with AsEnumerable method and on on part you can equalize both of them and select rows, then you can generate a DataTable from that query with CopyToDataTable method.
var dt = new DataTable();
var dc = new DataColumn() { ColumnName = "ID", DataType = typeof(string) };
dt.Columns.Add(dc);
dc = new DataColumn() { ColumnName = "Name", DataType = typeof(string) };
dt.Columns.Add(dc);
dt.Rows.Add(new object[] { "2", "A" });
dt.Rows.Add(new object[] { "4", "B" });
dt.Rows.Add(new object[] { "3", "C" });
dt.Rows.Add(new object[] { "5", "D" });
dt.Rows.Add(new object[] { "1", "E" });
List<string> order = new List<string>() { "1", "3", "2", "5", "4" };
var query = from item in order
join row in dt.AsEnumerable() on item equals row.Field<string>("ID")
select row;
var result = query.CopyToDataTable();
result will be;
I'm not sure this is the best way or not but this seems to fit with your case.
You can join both lists (the one with items and the one with sorted id's) and then select the items:
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
var list = new List<Item>{
new Item { Id = 2, Text = "A" },
new Item { Id = 4, Text = "B" },
new Item { Id = 3, Text = "C" },
new Item { Id = 5, Text = "D" },
new Item { Id = 1, Text = "E" }
};
var sortorder = new List<int> { 1, 3, 2, 5, 4 };
var sortedlist = sortorder.Join(list, x => x, y => y.Id, (x,y) => y);
foreach(var item in sortedlist)
Console.WriteLine("{0} {1}", item.Id, item.Text);
}
}
What is the best way to compare two lists based on values, order and the number of values. So all of the lists below should be different.
var list1 = new List<string> { "1", "2" };
var list2 = new List<string> { "2", "1" };
var list3 = new List<string> { "1", "2", "3" };
How about using SequenceEqual.
See http://ideone.com/yZeYRh
var a = new [] { "1", "2", "3" };
var b = new [] { "1", "2" };
var c = new [] { "2", "1" };
Console.WriteLine(a.SequenceEqual(b)); // false
Console.WriteLine(a.SequenceEqual(c)); // false
Console.WriteLine(c.SequenceEqual(b)); // false
It comes from the namespace System.Linq and can be used on any IEnumerable.
You can also pass it an IEqualityComparer to for example also do:
var d = new [] { "a", "B" };
var e = new [] { "A", "b" };
Console.WriteLine(d.SequenceEqual(e, StringComparer.OrdinalIgnoreCase)); // true
I like Zip for this, but you still need to manually compare Count.
lista.Count() ==listb.Count() && lista.Zip(listb, Equals).All(a=>a);
I am using a ListBox and the data is a class:
private class Duration
{
public int Value { get; set; }
public string Label { get; set; }
}
I bind the class in this way:
var durations = new List<Duration>()
{
new Duration() { Value = 5, Label = "5 minutes" },
new Duration() { Value = 10, Label = "10 minutes" },
new Duration() { Value = 15, Label = "15 minutes" },
new Duration() { Value = 30, Label = "30 minutes" },
new Duration() { Value = 45, Label = "45 minutes" },
new Duration() { Value = 60, Label = "1 hour" },
new Duration() { Value = 90, Label = "1 hour and half" }
};
this.listTime.DataSource = durations;
this.listTime.DisplayMember = "Label";
this.listTime.ValueMember = "Value";
Everything works fine and the labels are show.
When i go to read the selected value, I am not able to recover the value of the selected item.
I was expecting to be able to do this:
int value = listTime.SelectedItems[0].Value;
or at least this:
Duration value = listTime.SelectedItems[0];
but this gives me error, what I am doing wrong? How is the right way to get the value of the selected item on the ListBox?
if (listTime.SelectedIndex != -1)
{
var item = listTime.SelectedItem as Duration;
//or as suggested by 'Stu'
var item = (Duration)listTime.SelectedItem;
MessageBox.Show(item.Value.ToString());
}
If you use Listbox this code is Ok:
listTime.Items[0].Value