Adding then Referencing multiple values within IEnumerable - c#

I'm new to MVC. I have the following code in my method:
// Get list of news formats that were ticked
IEnumerable<int> newsFormatIds = viewModel.ClubNewsFormats
.Where(c => c.selected == true).
Select(c => c.NewsFormatId);
// Copy news formats
IEnumerable<ClubNewsFormat> clubNewsFormats = club.ClubNewsFormats.ToList();
// Remove previous formats
foreach (ClubNewsFormat previousClubNewsFormats in clubNewsFormats)
{
db.ClubNewsFormats.Remove(previousClubNewsFormats);
}
// Add new club news formats
foreach (int NewsFormatId in newsFormatIds)
{
NewsFormat newsFormat = db.NewsFormats.Find(NewsFormatId);
var newClubNewsFormat = new ClubNewsFormat {
ClubId = club.ClubId,
NewsFormatId = newsFormat.NewsFormatId,
Discount =
};
db.ClubNewsFormats.Add(newClubNewsFormat);
}
It gets a list of the items that were ticked in a view and then adds a record for each to the ClubNewsFormats table. By using the IEnumerable to generate a list of all the Model.ClubNewsFormats[c].NewsFormatId where Model.ClubNewsFormats[c].selected is true. As well as being able to select the the Id's you can also add a Discount for each Id. I want to be able to commit this Discount value for each Selected Id when saving them to the ClubNewsFormats Table.
How to I add the Discount to the IEnumerable as well as the Id so that I can commit it in the foreach (int NewsFormatId in newsFormatIds) loop?
The section of the view is:
#for (int c = 0; c < Model.ClubNewsFormats.Count(); c++)
{
<div>
#Html.HiddenFor(x => Model.ClubNewsFormats[c].NewsFormatId)
#Html.CheckBoxFor(x => Model.ClubNewsFormats[c].selected)
#Html.LabelFor(x => Model.ClubNewsFormats[c].Format, Model.ClubNewsFormats[c].Format)
#Html.EditorFor(x => Model.ClubNewsFormats[c].Discount)
</div>
}
The view model is:
public class ClubNewsFormatsViewModel
{
public int NewsFormatId { get; set; }
public string Format { get; set; }
public bool selected { get; set; }
[Display(Name = "Discount")]
public decimal Discount { get; set; }
}
which is referenced in the main view model by:
public ClubNewsFormatsViewModel[] ClubNewsFormats { get; set; }

I amended to controller to this:
// Remove previous formats
foreach (ClubNewsFormat previousClubNewsFormats in clubNewsFormats)
{
db.ClubNewsFormats.Remove(previousClubNewsFormats);
}
// Add new club news formats
foreach (ClubNewsFormatsViewModel clubNewsFormat in viewModel.ClubNewsFormats.Where(c => c.selected == true))
{
var newClubNewsFormat = new ClubNewsFormat { ClubId = club.ClubId, NewsFormatId = clubNewsFormat.NewsFormatId, Discount = clubNewsFormat.Discount };
db.ClubNewsFormats.Add(newClubNewsFormat);
}

Related

how to select a row on a table based on a condition in another row of another table?

I want to show only the rows from one table that meet a certain condition in another table. Example on the controller:
using(inventarioEntitiesDBA dc = new inventarioEntitiesDBA())
{
ProductandBodViewModel finalitem = new ProductandBodViewModel();
var m_List = dc.showcase.Where(x => x.prod_on_showcase > 0).ToList();
}
With the code above it's possible to display only the items that meets the condition, prod_on_showcase > 0. The ProductandBodViewModel is a model that I created to show two models on a same view.
The ProductandBodViewModel model:
public class ProductandBodViewModel
{
public List<inventory> inventory { get; set; }
public List<showcase> showcase { get; set; }
}
According to this condition (prod_on_showcase > 0) I want to show other information that is related to the table showcase table.
The model of the showcase table:
public class showcaseViewModel
{
public int id_inventory { get; set; }
public int prod_on_showcase { get; set; }
}
The model of the inventory table:
public partial class inventory
{
public int id_inventory { get; set; }
public Nullable<int> prod_on_inventory { get; set; }
public string prod_code { get; set; }
}
The relation between the tables is the id_inventory field.
In this case, the items displayed for the showcase table are 3, so the inventory table must also display 3 items.
With the code below it's possible to display info of the table showcase that meets the condition but with the other table (inventory) only display the first element that it's found according to the condition prod_on_showcase > 0.
using(inventarioEntitiesDBA dc = new inventarioEntitiesDBA())
{
ProductandBodViewModel finalitem = new ProductandBodViewModel();
var m_List = dc.showcase.Where(x => x.prod_on_showcase > 0).ToList();
var m_Each = dc.showcase.Where(e => e.prod_on_showcase > 0).Count();
for(int i = 0; i < m_Each; i++)
{
var prodEach = dc.inventory.Where(x => x.prod_on_showcase > 0).FirstOrDefault();
var b = dc.inventory.Where(a => a.id_inventory == prodEach.id_inventory).ToList();
finalitem.bod = b;
}
finalitem.showcase = m_List;
return View(finalitem);
}
You're asking var prodEach = dc.inventory.Where(x => x.prod_on_showcase > 0).FirstOrDefault(); to always return the First(or default) element in the array.
Linq's SelectMany is useful for what you're trying to do.
using(inventarioEntitiesDBA dc = new inventarioEntitiesDBA())
{
ProductandBodViewModel finalitem = new ProductandBodViewModel();
var m_List = dc.showcase.Where(x => x. > 0).ToList();
var m_List = showcase.Where(x => x.prod_on_showcase > 0).ToList();
finalitem.showcase = m_List.SelectMany(prodEach
=> dc.inventory.Where(a => a.id_inventory == prodEach.id_inventory)
finalitem.showcase = m_List;
return View(finalitem);
}```

GroupBy with linq entity poco

I have this entities
public class Counter
{
public int DocEntry { get; set; }
public int LineId { get; set; }
public string Item { get; set; }
public decimal Quantity { get; set; }
public string FromWarehouse { get; set; }
public string ToWarehouse { get; set; }
public virtual List<Batch> batchs { get; set; }
}
public class Batch
{
public string BatchNumber { get; set; }
public decimal Quantity { get; set; }
}
And I have List count,I should group the elements of the list based on the Item value, FromWarehouse, ToWarehouse, and the result should be grouped with summed quantity and the list of Batch merged, I tried cycling the Counter list using the foreach method and inserting in a new list the first element, in the subsequent iterations if the current row reflected the values of the one already in the list I summarized the quantities and added to the Batch list the elements of the Batch list of the i-th line, otherwise I added a new line, this approach I it seemed too complicated, not being very expert on linq or in any case is there an easier way to manage the group by?
This is my method:
public static List<Counter> GroupBy(List<Counter> list)
{
List<Counter> rows = new List<Counter>();
int i = 0;
foreach (Counter elem in list)
{
if (list.First() == elem)
{
rows.Add(elem);
i++;
}
else
{
if (elem.Item == rows.ElementAt(i).Item &&
elem.FromWarehouse == rows.ElementAt(i).FromWarehouse &&
elem.ToWarehouse == rows.ElementAt(i).ToWarehouse)
{
rows.First().Quantity += elem.Quantity;
rows.First().batchs.Add(elem.batchs.First());
}
else
{
rows.Add(elem);
i++;
}
}
}
return rows;
}
This is the solution:
public static List<Counter> GroupBy(List<Counter> list)
{
List<Counter> rows = new List<Counter>();
foreach (Counter elem in list)
{
if (rows.Any(x=>x.Item == elem.Item &&
x.FromWarehouse == elem.FromWarehouse &&
x.ToWarehouse == elem.ToWarehouse))
{
rows.First().Quantity += elem.Quantity;
rows.First().batchs.Add(elem.batchs.First());
}
else
{
rows.Add(elem);
}
}
return rows;
}
Let's try to write the Linq, step by step.
"I have List count":
List<Counter> count = ...
"I should group the elements of the list based on the Item value, FromWarehouse, ToWarehouse":
var result = count
.GroupBy(item => new {
item.Item,
item.FromWarehouse
item.ToWarehouse
})
"result should be grouped with summed quantity and the list of Batch merged", i.e. you should Aggregate items within each chunk
var result = count
.GroupBy(item => new {
item.Item,
item.FromWarehouse
item.ToWarehouse
})
.Select(chunk => new {
key = chunk.Key,
summary = chunk.Aggregate(
Tuple.Create(0m, new List<Batch>()), // initial empty
(s, a) => Tuple.Create(s.Item1 + a.Quantity, // add
s.Item2.Concat(a.batchs).ToList()) // merge
})
Finally, let's represent the result in more convenient (readable) format:
var result = count
.GroupBy(item => new {
item.Item,
item.FromWarehouse
item.ToWarehouse
})
.Select(chunk => new {
key = chunk.Key,
summary = chunk.Aggregate(
Tuple.Create(0m, new List<Batch>()),
(s, a) => Tuple.Create(s.Item1 + a.Quantity,
s.Item2.Concat(a.batchs).ToList())
})
.Select(item => new {
Item = item.key.Item,
FromWarehouse = item.key.FromWarehouse,
ToWarehouse = item.key.ToWarehouse,
Quantity = item.summary.Item1,
Batches = item.summary.Item2
}); // Add ToArray() if you want to materialize

Unable to display LINQ query output values from controller to view

Controller
public ActionResult Track(string awb)
{
ViewBag.Title = "Track Your Shipment";
ViewBag.ErrorMessage = string.Empty;
ViewBag.ShipmentNo = awb;
FLCourierDetail trackOutput = new FLCourierDetail();
if (awb != null)
{
trackOutput = db.FL_CourierDetail.SingleOrDefault(fLCourierDetail => fLCourierDetail.AWBNumber == awb);
if (trackOutput != null)
{
var courierId = db.FL_CourierDetail.Where(s => s.AWBNumber == awb).Select(s => s.Courier);
var currentStatus = (from c in db.FL_CourierDetail
join s in db.FL_CourierStatus
on c.Courier equals s.CourierId
where c.AWBNumber == awb
select new { awb = c.AWBNumber, staus = s.StatusId, updated = s.StatusId, remark = s.Remark }).ToList();
ViewBag.CurrentStatus = currentStatus;
}
else
{
ViewBag.ErrorMessage = "Shipment number not found.";
}
}
else
{
ViewBag.ErrorMessage = "Please provide valid Shipment number.";
}
return View(trackOutput);
}
View
<div class="col-md-6">
#{
var status = ViewBag.CurrentStatus;
foreach (var item in status)
{
<p>#item</p>
}
}
</div>
If I iterate using foreach or if loop I am able to see the data in debug, but I am not able to write in html.
Debug
Web Page
Error
I am not able to read each value like awb, status, date etc.
Did I miss anything here?
The query result is an anonymous class, within the loop, each item is an object and thus the exception, object has now awb property.
One way to solve this is by defining a class:
public class Status {
public string awb { get; set; }
public int staus { get; set; }
public int updated { get; set; }
public string remark { get; set; }
}
Then your select would look like:
... select new Status { awb = c.AWBNumber, staus = s.StatusId, updated = s.StatusId, remark = s.Remark }).ToList();
Then, within the View:
var status = (List<Status>) ViewBag.CurrentStatus;
Another possible solution is to use strongly typed view model
Firstly, you could create a Model class for returned data;
var currentStatus = (from c in db.FL_CourierDetail
join s in db.FL_CourierStatus
on c.Courier equals s.CourierId
where c.AWBNumber == awb
select new CurrentStatus { awb = c.AWBNumber, staus = s.StatusId, updated = s.StatusId, remark = s.Remark }).ToList();
public class CurrentStatus
{
public string awb { get; set; }
public int staus { get; set; }
public int updated { get; set; }
public string remark { get; set; }
}
Secondly, you can't get output an entire object, you should specify properties which you want to display;
<div class="col-md-6">
#{
var status = (List<CurrentStatus>) ViewBag.CurrentStatus;
foreach (var item in status)
{
<p>#item.awb</p>
<p>#item.staus</p>
<p>#item.updated</p>
<p>#item.remark</p>
}
}
</div>
This is really strange, but in console application the following code actually works:
dynamic even = new List<int> { 1, 2, 3, 4 }
.Where(x => x % 2 == 0)
.Select((x, index) => new { Position = index, Num = x });
// Output:
// Position: 0, Number: 2
// Position: 1, Number: 4
foreach (dynamic item in even)
{
Console.WriteLine($"Position: {item.Position}, Number: {item.Num}");
}
However, in ASP.NET ths doesn't work, and I don't really understand because ViewBag is dynamic, too.
UPDATE
Same question was asked here. A quote from there:
You're returning an instance of an anonymous type. If you weren't using dynamic, your only choice here would be to return an object - the anonymous type is unknown outside of your own function. If you had a variable of type object, you'd get a compile time error that it doesn't have a LogoName property. All you've done with dynamic is defer exactly the same lookup rules until runtime. At runtime, the best type that can be determined is object.
As this answer states, the following must not work, but it works:
static void DoWork()
{
dynamic evens = GetEvens();
foreach (dynamic item in evens)
Console.WriteLine($"Position: {item.Position}, Number: {item.Num}");
}
static dynamic GetEvens() =>
new List<int> { 1, 2, 3, 4 }
.Where(x => x % 2 == 0)
.Select((x, index) => new { Position = index, Num = x });
In this case I return dynamic. However, the code work correctly.

Group By query in mvc5

Hi i want to write sql Group by query in C# of my MVC5 application.
In the above image I have group by query which i wrote in sql . That I want to write in C# front end.
I tried to write query in front end. But I am getting error which is mentioned in the image. Now I want to write that Group By query in C# and want to display the each employee with count (output same as mentioned in the first image). Can anyone help me to resolve this issue?
My ViewModel(Dashnboard View model)
public class DashboardViewmodel
{
public List<CustomerTypeCountModel> CustomerTypesCountModels { get; set; }
public List<View_VisitorsForm> Visits { get; set; }
public CustomerTypeViewModel CustomerTypeViewModels { get; set; }
public int sizingcount { get; set; }
public int Processingcount { get; set; }
//here i declared two properties
public string EmployeeName { get; set; }
public string EmployeeCount { get; set; }
}
My Controller code
[HttpGet]
public ActionResult SalesVisit()
{
return View();
}
public ActionResult GetDatesFromSalesVisit(DashboardViewmodel dvm)
{
var fromdate = Convert.ToDateTime(dvm.CustomerTypeViewModels.FromDate);
var todate = Convert.ToDateTime(dvm.CustomerTypeViewModels.ToDate);
List<View_VisitorsForm> empcount = new List<View_VisitorsForm>();
if (DepartmentID == new Guid("47D2C992-1CB6-44AA-91CA-6AA3C338447E") &&
(UserTypeID == new Guid("106D02CC-7DC2-42BF-AC6F-D683ADDC1824") ||
(UserTypeID == new Guid("B3728982-0016-4562-BF73-E9B8B99BD501"))))
{
var empcountresult = db.View_VisitorsForm.GroupBy(G => G.Employee)
.Select(e => new
{
employee = e.Key,
count = e.Count()
}).ToList();
empcount = empcountresult ;//this line i am getting error
}
DashboardViewmodel obj = new DashboardViewmodel();
return View("SalesVisit", obj);
}
When you use a GroupBy you get an IEnumerable<IGrouping<Key,YourOriginalType>> so you do not have .Employee and .VisitingID properties.
Change as following:
public class EmployeeCount
{
public string Employee {get; set;}
public int Count {get; set;}
}
List<EmployeeCount> result = db.View_VisitorsForm
.Where(item => item.VisitingDate >= beginDate && item.VisitingDate < endDate)
.GroupBy(G => G.Employee)
.Select(e =>new EmployeeCount
{
employee = e.Key,
count = e.Count()
}).ToList();
//Now add the result to the object you are passing to the View
Also keep in mind that you are not instantiating objects of type View_VisitorsForm but an anonymous object so assigning the result to empcount yet alone with the added FirstOrDefault will not compile
To pass this structure to the View and present it check this question
hope this helps you
var query = db.View_VisitorsForm.Where(o => o.VisitingDate >= new DateTime(2016,10,01) && o.VisitingDate <= new DateTime(2016, 10, 30)).GroupBy(G => G.Employee)
foreach (var item in query)
{
Console.WriteLine($"Employee Id {item.Key} : Count :{item.Count()}");
}

Radiobutton acting like a checkbox MVC

With the code below, i can select multiple radio buttons at the same time, that is a problem, a true radio button only works with ONE selected item. How do i re-arrange my code so that it acts like a real radio button and not like a checkbox like the code below?
for (int i = 0; i < Model.RadioButtonItems.Count; i++)
{
<div>
#Html.HiddenFor(m => m.RadioButtonItems[i].RBName)
#Html.LabelFor(l => l.RadioButtonItems[i].RBIsSelected, Model.RadioButtonItems[i].RBName)
#Html.RadioButtonFor(r => r.RadioButtonItems[i].RBIsSelected, true);
</div>
}
The rest of code:
Model:
public class ModelVariables
{
public List<Item> RadioButtonItems { get; set; }
}
public class Item
{
public string RBName { get; set; }
public bool RBIsSelected { get; set; }
}
public static class Repository
{
public static List<Item> RBFetchItems()
{
return new List<Item>()
{
new Item() {RBName = "Metal"},
new Item() {RBName = "Jazz"},
new Item() {RBName = "Trance"}
};
}
}
Controller:
var selectedRBItems = model.RadioButtonItems.Where(x => x.RBIsSelected).Select(x => x.RBName).ToList();
if (model.RadioButtonItems != null && selectedRBItems.Count > 0)
{
ViewBag.RBResults = "Successfully Logged Pressed RB's!";
}
else
{
ViewBag.RBResults = "You must select a radio button!";
}
Summary: this code let's you select multiple radiobuttons, i DONT want that, i want only one possible selection out of many options.
Each radio button has a different name attribute so they are not grouped. Your model needs a property to bind the selected value to, and a collection of items for the options
public class ModelVariables
{
[Required(ErrorMessage = "...")]
public string SelectedValue { get; set; }
public List<string> Options { get; set; }
}
and in the GET method
var model = new ModelVariables()
{
Options = new List<string>() { "Metal", "Jazz", "Trance" },
SelectedValue = ? // set this if you want one of the buttons initially selected
};
return View(model);
and in the view
foreach (var option in Model.Options)
{
<label>
#Html.RadionButtonFor(m => m.SelectedValue, option, new { id = "" })
<span>#option</span>
</label>
}
// add the following if you do not set an initial value in the GET method
#Html.ValidationMessageFor(m => m.SelectedValue)

Categories

Resources