need to combine to lists in a view - c#

the two lists to be passed to the view from controller.
ViewBag.mylist1 = new Accounts().List(Category.Income);
ViewBag.mylist2 = new Accounts().List(Category.Expenditure);
HOw to combine thse two lists in a single view?

myList1 = myList1.Concat(myList2).ToList();
Concat returns an IEnumerable that is the two lists put together, it doesn't modify either existing list. Also, since it returns an IEnumerable, if you want to assign it to a variable that is List, you'll have to call ToList() on the IEnumerable that is returned.

You can use also AddRange()
ViewBag.myList1 = ViewBag.myList1.AddRange(ViewBag.myList2);
AddRange() modifies the list by adding the other items to it.
Concat() returns a new sequence containing the list and the other items, without modifying the list.

List<SelectListItem> lstFuelType = new List<SelectListItem>();
var FuelType = dbContext.APPL_COMMON_MST.AsNoTracking().AsQueryable().Where(cm);
lstFuelType = new SelectList(FuelType, "KEY", "VALUE").ToList();
lstFuelType.Insert(0,new SelectListItem { Text = "ALL", Value = "ALL", Selected = false });
ViewBag.FuelType = lstFuelType;

Related

How to add Items in IEnumerable?

I have this code and I would like to add Item in the list and I am doing like this.
IEnumerable<SelectListItem> note = new List<SelectListItem>();
var selectList = new SelectListItem
{
Text = Convert.ToString(amount.Key),
Value Convert.ToString(amount.Value)
};
note.ToList().Add(selectList);
I am unable to Add item.
The ToList() call creates a brand new list, which is not connected to the original list. No changes you make to the new list will be propagated back to the original.
As for adding, you cannot add items to the IEnumerable<T>. It all depends on that you're trying to do with your code, but why do you use IEnumerable<SelectListItem> instead of IList<SelectListItem>, which has an Add() operation. Since IList<T> is-a IEnumerable<T>, you will be able to pass the note to any method that expects an IEnumerable<T>.
When you say ToList() it's not casting it, it's creating a whole new list and that's what you're adding to.
What you want is to avoid the weird casting you do to start with (make note a List not an IEnumerable) and then add to that directly
List<SelectListItem> note = new List<SelectListItem>()
var selectList = new SelectListItem{Text = Convert.ToString(amount.Key), Value Convert.ToString(amount.Value) }
note.Add(selectList)
Not that it makes any sense to do that but if you really want to assign your list to an IEnumerable to begin with you can still cast it later when you need the added functionality as follow:
IEnumerable<SelectListItem> note = new List<SelectListItem>()
var selectList = new SelectListItem{Text = Convert.ToString(amount.Key), Value Convert.ToString(amount.Value) }
((List<SelectListItem>)note).Add(selectList)
List<SelectListItem> note = new List<SelectListItem>();
var selectList = new SelectListItem
{
Text = Convert.ToString(amount.Key),
Value Convert.ToString(amount.Value)
};
note.Add(selectList);
var notes = note.AsNumerable();

Sorting list of items according to another fixed list

I'm very beginner in C#.
I have a List of
private List<String> mXMLProdcutsIDs = new List<String>();
mXMLProductsIDs is like
{"megapack", "levelpack","bikepack"}.
Sometimes I get another list which is in random order, {"megapack", "levelpack","bikepack"}
I would like to resort that list according to the mXMLProductIDs list order
You can use index of string in original list to define items order:
var result = anotherList.OrderBy(s => mXMLProdcutsIDs.IndexOf(s));
Shorten version:
var result = anotherList.OrderBy(mXMLProdcutsIDs.IndexOf);
Keep in mind, that result will be IEnumerable<string>. You can use ToList() if you need to save results in list.
Another option (if you don't have duplicated items in another list):
var result = mXMLProdcutsIDs.Intersect(anotherList);

Compare to list<String> using linq?

I have 3 list List
ListMaster contains {1,2,3,4,....} ..getting populated from DB
List1 contains {1,3,4}
List2 contains {1,3,95}
how to check Which list items are present in master list using linq
var inMaster = List1.Intersect(ListMaster);
or for both list :
var inMaster = List1.Intersect(List2).Intersect(ListMaster);
check if any item from list1, list2 exist in master
var existInMaster = inMaster.Any();
You can use Enumerable.Intersect:
var inMaster = ListMaster.Intersect(List1.Concat(List2));
If you want to know which are in List1 which are not in the master-list, use Except:
var newInList1 = List1.Except(ListMaster);
and for List2:
var newInList2 = List2.Except(ListMaster);
Can i use a list .all to check all item of a list in another list for
list of string
So you want to know if all items of one list are in another list. Then using Except + Any is much more efficient(if the lists are large) because Intersect and Except are using sets internally whereas All loops all elements.
So for example, does the master-list contain all strings of List1 and List2?
bool allInMaster = !List1.Concat(List2).Except(ListMaster).Any();
You can use Enumerable.Intersect method like;
Produces the set intersection of two sequences by using the default
equality comparer to compare values.
var inMaster1 = List1.Intersect(ListMaster);
var inMaster2 = List2.Intersect(ListMaster);
Here is a DEMO.

list.add seems to be adding a reference to the original object?

I have created a couple custom classes (NTDropDown and NTBaseFreight) which I use to store data that I retrieve from a DB. I initialize a List of NTBaseFreight and 2 lists for NTDropDown.
I can successfully use List.Add to add freights to the freights list, but as I debug the code, my 2 dropdown lists contain only 1 NTDropDown, which always has the same values as NTDropDown (I'm assuming this is a referencing problem, but what am I doing wrong)?
To give an example, on the second row, if the carrier and carrier_label were "001", "MyTruckingCompany" and I put a break on the if statement for frt_carriers, both frt_carriers and frt_modes would contain only 1 item in their list, with the values "001", "MyTruckingCompany"...the same values in NTDropDown.
Code:
List<NTDropDown> frt_carriers = new List<NTDropDown>();
List<NTDropDown> frt_modes = new List<NTDropDown>();
List<NTBaseFreight> freights = new List<NTBaseFreight>();
NTDropDown tempDropDown = new NTDropDown();
NTBaseFreight tempFreight = new NTBaseFreight();
//....Code to grab data from the DB...removed
while (myReader.Read())
{
tempFreight = readBaseFreight((IDataRecord)myReader);
//check if the carrier and mode are in the dropdown list (add them if not)
tempDropDown.value = tempFreight.carrier;
tempDropDown.label = tempFreight.carrier_label;
if (!frt_carriers.Contains(tempDropDown)) frt_carriers.Add(tempDropDown);
tempDropDown.value = tempFreight.mode;
tempDropDown.label = tempFreight.mode_label;
if (!frt_modes.Contains(tempDropDown)) frt_modes.Add(tempDropDown);
//Add the freight to the list
freights.Add(tempFreight);
}
Yes, a list of reference types is actually just a list of references.
You have to create a new instance for each object that you want to store in the list.
Also, the Contains method compares references, so two objects containing the same data are not considered to be equal. Look for a value in the properties of the objects in the list.
if (!frt_carriers.Any(c => c.label == tempFreight.carrier_label)) {
NTDropDown tempDropDown = new NTDropDown {
value = tempFreight.carrier,
label = tempFreight.carrier_label
};
frt_carriers.Add(tempDropDown);
}
tempDropDown is the same object throughout the whole loop. You will need to create a new instance of it if you want to add more than one.
I'm having a hard time trying to figure out what exactly your'e trying to do with adding that tempDropDown the the list.

How to remove an object from a list collection?

I had to change on my lines of code around. I before had something like this
// this is in a static method.
List<string> mySTring = new List<string>();
mySTring.add("one");
mySTring.add("two");
However on one of my pages I have a dropdownlist that does not require the field "two" so instead of writing duplicate code all I did was
myString.remove("two");
Now I need to change my list to a List<SelectListItem> myList = new List<SelectListItem>();
So I have it now looking like this:
List<SelectListItem> myList = new List<SelectListItem>()
{
new SelectListItem() { Text = "one", Value = "one"},
new SelectListItem() { Text = "two", Value = "two"},
};
So now how do I remove the selectListItem that contains "two"? I know I probably could use remove by index. But I might add to list in the future so I don't want to start hunting down and changing it if the index changes.
Thanks
List<T> is, by default, going to be comparing object references (unless SelectListItem implements a custom equality method). So unless you still have a reference to the second item around, you are going to have to get it either by reference, or by finding the desired item:
var item = myList.First(x=>x.Value == "two");
myList.Remove(item);
Index may be easier...
You could use the RemovalAll method:
myList.RemoveAll(i => i.Text == "two");
Obviously this will get rid of all the items whose "Text" property is "two", but since you're using it in a ComboBox I'm assuming you'll only have one item for each "Text" value.
we can make mouse selection over the autocomplete div by adding this following code
into the jquery autocomplete function
here i have used the id name as Requiredid
$("#Requiredid").autocomplete({focus:function(e,ui) {
document.getElementById('Requiredid').value = ui.item.label;
document.getElementById('hdnValue').value = ui.item.id;//If You need this value you //can add this line
}});

Categories