Reorganize List into ListBox after adding a new Item - c#

I have list in listbox where I want to order by a field once I added item to it:
var lstdata = (List<EmployeeAssignationModel>)lstTechToNotified.DataSource;
lstdata.Add(new EmployeeAssignationModel()
{
UserName = selectedItem.UserName,
EmpGuid = selectedItem.EmpGuid,
Name = selectedItem.Name,
Abbreviation = selectedItem.Abbreviation
});
lstTechToNotified.DataSource = null;
lstTechToNotified.DisplayMember = "Abbreviation";
lstTechToNotified.ValueMember = "UserName";
lstTechToNotified.DataSource = lstdata;
lstTechToNotified.Refresh();
So I try adding OrderBy once item is added like:
var lstdata = (List<EmployeeAssignationModel>)lstTechToNotified.DataSource;
lstdata.Add(new EmployeeAssignationModel()
{
UserName = selectedItem.UserName,
EmpGuid = selectedItem.EmpGuid,
Name = selectedItem.Name,
Abbreviation = selectedItem.Abbreviation
});
lstdata.OrderBy(x => x.Abbreviation);
lstTechToNotified.DataSource = null;
lstTechToNotified.DisplayMember = "Abbreviation";
lstTechToNotified.ValueMember = "UserName";
lstTechToNotified.DataSource = lstdata;
lstTechToNotified.Refresh();
But it just don't update, it always send item added to bottom of the list. What am I doing wrong?

OrderBy returns a new list, rather than doing the changes in place:
lstdata = lstdata.OrderBy(x => x.Abbreviation).ToList();
Try this instead.
You'll see here it returns a collection: https://msdn.microsoft.com/en-us/library/bb534966(v=vs.110).aspx

Related

How to convert Linq result to viewmodel

while filling a combobx, I need to convert a Linq-result to a viewmodel.
Actually, I query the records and then I fill a list of the viewmodel in a loop, but that seems to be a bit strange:
public static IEnumerable<ComboBoxActivities> GetActivitySelectList()
{
using(ApplicationDbContext db = new ApplicationDbContext())
{
var result = from activity in db.Activities
where activity.Available
select new
{
ActivityId = activity.Id,
ActivityName = activity.ActivityName,
Available = activity.Available
};
List<ComboBoxActivities> list = new List<ComboBoxActivities>();
foreach(var res in result)
{
ComboBoxActivities listItem = new ComboBoxActivities()
{
ActivityId= res.ActivityId,
ActivityName= res.ActivityName,
Available= res.Available
};
list.Add(listItem);
}
return list;
}
}
Is this really the right way?
I also tried:
var result = from activity in db.Activities
where activity.Available
select new ComboBoxActivities()
{
ActivityId = activity.Id,
ActivityName = activity.ActivityName,
Available = activity.Available
};
But then my razorview crashes with the message that direct binding to a quers (DbSet, DbQuery...) is not supported.
You can convert the IEnumerable<T> to a List<T> by using ToList()
public static List<ComboBoxActivities> GetActivitySelectList()
{
using(ApplicationDbContext db = new ApplicationDbContext())
{
var result = from activity in db.Activities
where activity.Available
select new ComboBoxActivities()
{
ActivityId = activity.Id,
ActivityName = activity.ActivityName,
Available = activity.Available
};
return result.ToList();
}
}
As for loading a ComboBox from a table query, ComboBox has a DataSource property which you can assign the List to.

Using LINQ to build a cascading list in C#

I have a need to display a list of departments and sub-departments. I am loading the list of departments properly.
At this time, I have:
var deparmentList = Departments.LoadList();
var departments = new List<ListItem>(
from department in departmentList
select new ListItem {
Text = department.Name,
Value = department.Id.ToString(),
IsSelected = department.IsActive
}
);
I now need to load the list of sub-departments. Each sub-department has an Id, DepartmentId, and Name. However, I only want to get the sub-departments associated with departments that are selected. Currently, I have the following:
var subDepartmentList = SubDepartment.LoadList();
var subdepartments = new List<ListItem>(
from subdepartment in subDepartmentList
// where ?
select new ListItem {
Text = subdepartment.Name,
Value = subdepartment.Id.ToString(),
IsSelected = false
}
);
I'm not sure how to do the join or filter between the two. How do I do this in LINQ?
var selectedDepartmentSubDepartments =
from dep in departments
join subDep in subDepartmentList
on dep.Value equals subDep.Id.ToString()
where dep.IsSelected
select new ListItem {
Text = subDep.Name,
Value = subDep.Id.ToString(),
IsSelected = false
};
var subdepartments = new List<ListItem>(selectedDepartmentSubDepartments);
You can add a where clause that checks if at least one associated and 'selected' department exists:
var subDepartmentList = SubDepartment.LoadList();
var subdepartments = new List<ListItem>(
from subdepartment in subDepartmentList
where departments.Any(x => x.IsSelected &&
x.Value == subdepartment.DepartmentId.ToString())
select new ListItem {
Text = subdepartment.Name,
Value = subdepartment.Id.ToString(),
IsSelected = false
}
);

create NetSuite ItemFulfillment containg items with lot/serial info

I am using SuiteTalk web services (v. 2013_2) . I am trying to create an ItemFulfillment where the items in it were related to items that had a lot or serial number.
When I try to save this item fulfillment into NetSuite I get an error of :
Please commit inventorydetail on this line.
I was attempting to set the itemFulfillment.serialNumbers and itemFulfillment.binNumbers when I create the itemFulfillmentItem.
For example I set
nsIfItem.serialNumbers = "SNum(5)"
nsIfItem.binNumbers = "BNum(5)"
based on those properties being- A comma delimited list of serial or LOT numbers. If entering serial numbers there must be a number for each item.
Lot numbers must be entered in a format of LOT#(Quantity).
For example, to enter a quantity of 100 items as Lot number ABC1234, enter ABC1234(100).
Do I also need to set something else on the itemFulfillment or how do I get rid of that error.
I'm not sure if this question is still active, but I had the same issue and iI couldn't find much help on it. I solved this issue by creating the inventory assignment objects and adding to the transaction.
First, create the initialize ref for Item Fulfillment and assign the returned record to a variable:
InitializeRecord ir = new InitializeRecord();
ir.type = InitializeType.itemFulfillment;
InitializeRef iref = new InitializeRef();
iref.typeSpecified = true;
iref.type = InitializeRefType.salesOrder;
iref.internalId = 'Sales Order internalID';
ir.reference = iref;
ReadResponse getInitResp = _service.initialize(ir);
ItemFulfillment ifrec = (ItemFulfillment)getInitResp.record;
Get the list of items on the initialized transaction:
ItemFulfillmentItemList ifitemlist = ifrec.itemList;
Create a list to which to add each unique item being fulfilled:
List<ItemFulfillmentItem> ifitems = new List<ItemFulfillmentItem>();
Run the following code for each item in initialized transaction's item list:
If the current line item has already been added to the ifitems list, add the current Fulfillment line as an assignment to that item:
InventoryAssignment assignment = new InventoryAssignment
{
issueInventoryNumber = new RecordRef { internalId = 'internalID',
type = 'RecordType',
typeSpecified = true
}
};
List<InventoryAssignment> list = new List<InventoryAssignment>();
list.Add(assignment);
ifitemlist.item[b].inventoryDetail = new InventoryDetail
{
inventoryAssignmentList = new InventoryAssignmentList
{
inventoryAssignment = list.ToArray()
}
};
ifitemlist.item[b].quantity += 'quantity shipped';
If the line item has not yet been added, create new line item:
ItemFulfillmentItem ffItem = new ItemFulfillmentItem();
ffItem.item = ifitemlist.item[b].item;
ffItem.itemReceive = true;
ffItem.itemReceiveSpecified = true;
ffItem.itemIsFulfilled = true;
itemIsFulfilled = true;
ffItem.itemIsFulfilledSpecified = true;
ffItem.orderLineSpecified = true;
ffItem.orderLine = ifitemlist.item[b].orderLine;
//Check if serialized
if (Your fulfillment item contains serialized data)
{
ffItem.serialNumbers = 'Serial numbers';
InventoryAssignment assignment = new InventoryAssignment
{
issueInventoryNumber = new RecordRef {
internalId = 'Inventory internal ID',
type = RecordType,
typeSpecified = true
}
};
ffItem.inventoryDetail = new InventoryDetail
{
inventoryAssignmentList = new InventoryAssignmentList
{
inventoryAssignment = new InventoryAssignment[]
{
assignment
},
replaceAll = false
},
nullFieldList = new string[] { },
customForm = new RecordRef { }
};
}
ffItem.quantity = 'QUANTITY SHIPPED';
ffItem.quantitySpecified = true;
ifitems.Add(ffItem);
Finally, add your "ifitems" list to your Item Fulfillment and add this to NetSuite:
ItemFulfillmentItemList ifitemlistToFulfill = new ItemFulfillmentItemList();
ifitemlistToFulfill.replaceAll = false;
ifitemlistToFulfill.item = ifitems.ToArray();
ItemFulfillment newItemFulfill = new ItemFulfillment();
newItemFulfill.itemList = ifitemlistToFulfill;
_service.add(newItemFulfill);

Entity Framework add first row to database from a List?

foreach (StockItem item in StockList)
{
Master master = new Master();
master.VoucherNo = BillNo;
master.Voucher = "Sales";
master.StockName = StockList[0].StockName;
master.Quantity = StockList[0].Quantity;
master.Unit = StockList[0].Unit;
master.Price = StockList[0].UnitPrice;
master.Amount = StockList[0].Amount;
dbContext.AddToMasters(master);
dbContext.SaveChanges();
}
Sale sale = new Sale();
sale.InvoiceNo = BillNo;
sale.Date = BillDate;
sale.Party = Customer;
sale.Amount = (decimal)TotalAmount;
dbContext.AddToSales(sale);
dbContext.SaveChanges();
This code add only the first row from StockList for all n times if there are n rows.
What wrongs with the code?
You are iterating over StockList, but you aren't actually using the iteration variable.
Everywhere that you use StockList[0], you should be using item.
Edit: Here is what your loop should look like:
foreach (StockItem item in StockList)
{
Master master = new Master();
master.VoucherNo = BillNo;
master.Voucher = "Sales";
master.StockName = item.StockName;
master.Quantity = item.Quantity;
master.Unit = item.Unit;
master.Price = item.UnitPrice;
master.Amount = item.Amount;
dbContext.AddToMasters(master);
dbContext.SaveChanges();
}

Is there a way to combine 2 LINQ2XML queries?

var instructions = (from item in config.Elements("import")
select new
{
name = item.Attribute("name").Value,
watchFolder = item.Attribute("watchFolder").Value,
root = item.Element("documentRoot").Value,
DocumentNameDynamic = item.Element("documentName").Attribute("xpath").Value,
DocumentNameStatic = item.Element("documentName").Attribute("static").Value,
TemplateName = item.Element("template").Attribute("template").Value,
Path = item.Element("path").Attribute("path").Value,
fields = item.Element("fields").Elements()
}).SingleOrDefault();
var fields = from item in instructions.fields
select new
{
xpath = item.Attribute("xpath").Value,
FieldName = item.Attribute("FieldName").Value,
isMultiValue = bool.Parse(item.Attribute("multiValue").Value)
};
I think something like this should work. I added the Select method to return the anonymous class.
var instructions = (from item in config.Elements("import")
select new
{
name = item.Attribute("name").Value,
watchFolder = item.Attribute("watchFolder").Value,
root = item.Element("documentRoot").Value,
DocumentNameDynamic = item.Element("documentName").Attribute("xpath").Value,
DocumentNameStatic = item.Element("documentName").Attribute("static").Value,
TemplateName = item.Element("template").Attribute("template").Value,
Path = item.Element("path").Attribute("path").Value,
fields = item.Element("fields").Elements().Select(item => new {
xpath = item.Attribute("xpath").Value,
FieldName = item.Attribute("FieldName").Value,
isMultiValue = bool.Parse(item.Attribute("multiValue").Value)
}
).SingleOrDefault();
If you don't want to use the Select Extension method, you can use LINQ syntax. Here is an example of this.
var instructions = (from item in config.Elements("import")
select new
{
name = item.Attribute("name").Value,
watchFolder = item.Attribute("watchFolder").Value,
root = item.Element("documentRoot").Value,
DocumentNameDynamic = item.Element("documentName").Attribute("xpath").Value,
DocumentNameStatic = item.Element("documentName").Attribute("static").Value,
TemplateName = item.Element("template").Attribute("template").Value,
Path = item.Element("path").Attribute("path").Value,
fields = from e in item.Element("fields").Elements()
select new {
xpath = item.Attribute("xpath").Value,
FieldName = item.Attribute("FieldName").Value,
isMultiValue = bool.Parse(item.Attribute("multiValue").Value)
} // End of inner select statement
} // End of outer select statement
).SingleOrDefault();

Categories

Resources