I'm trying to get sum of all items of cars. But i'm just getting sum of each items, which you can see here Link
So how can I get sum of all items, like the sum is (4), instead of getting each items?
Controller:
public ActionResult Home()
{
var model = new CompositeModel();
model.CountCars = getCountOfCars();
return View(model);
}
private IEnumerable<CountCarsrCountView> getCountOfCars()
{
var countCars = this.Data.Cars.All()
.Select(t => new CountCarsrCountView
{
counter = t.Cars.Count()
});
return countCars ;
}
ViewModel
public class CountCarsViewModel
{
public int counter { get; set; }
}
CompositeModel
public class CompositeModel
{
public IEnumerable<CountCarsViewModel> CountCars { get; set; }
}
View
#model CompositeModel
<div class="container">
#for (int i = 0; i < Model.CountCarsViewModel.Count(); i++)
{
var cars = Model.CountCarsViewModel.ElementAt(i);
<p>#cars.counter</p>
}
</div>
Car model:
public class Car
{
[Key]
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public int Price { get; set; }
public virtual Category Category { get; set; }
public int CategoryId { get; set; }
public int FilePathId { get; set; }
public string FileName { get; set; }
public string UserId { get; set; }
public virtual User Users { get; set; }
}
You can use Linq as so:
public class MyItem
{
public int MyCount { get; set; }
}
public List<MyItem> AllItems { get; set; }
In this example if you want the count of all items in the list you would do this:
var count = AllItems.Count();
If you wanted to sum the Counts of all items in the list you would do this:
var count2 = AllItems.Sum(a => a.MyCount);
Example:
AllItems = new List<UserQuery.MyItem>()
{
new MyItem(){ MyCount = 3 },
new MyItem(){ MyCount = 4 },
};
var count = AllItems.Count(); // This would be 2
var count2 = AllItems.Sum(a => a.MyCount); // This would be 7
Related
I am new to programming. I want to list the categories whose nested number is not clear. As a result of my research, I decided that I could do it in a single Database table. I thought I should use a recursive structure for this and decided to use partialview for it.
DB Entity
public class Categories {
public int ID { get; set; }
public int ParentCatID { get; set; }
public string CategoryName { get; set; }
public string Instruction { get; set; }
}
PartialView I am trying to write
Can I do it without using PartialView?
I want to achieve this output
class
public class Categories
{
public int ID { get; set; }
public int? ParentCatID { get; set; }
public Categories ParentCat { get; set; }
public string CategoryName { get; set; }
public string Instruction { get; set; }
public IList<Categories> Children { get; protected set; } = new List<Categories>();
}
ViewModel
public class CategoriesViewModel
{
public CategoriesViewModel()
{
ChildItems = new List<CategoriesViewModel>();
}
public long ID { get; set; }
public string CategoryName { get; set; }
public string Instruction { get; set; }
public CategoriesViewModel ParentCatID { get; set; }
public IList<CategoriesViewModel> ChildItems { get; set; }
public void AddChildItem(CategoriesViewModel childItem)
{
childItem.ParentCatID = this;
ChildItems.Add(childItem);
}
}
controller
public IActionResult List()
{
var categories = _context.ToList(); // example
var categoryItems = new List<CategoriesViewModel>();
var topCategories = categories.Where(x => !x.ParentCatID.HasValue);
foreach (var category in topCategories)
{
var categoryMenuItem = Map(category);
categoryItems.Add(categoryMenuItem);
}
return View(categoryItems);
}
private CategoriesViewModel Map(Categories category)
{
var categoryMenuItem = new CategoriesViewModel
{
ID = category.ID,
CategoryName = category.CategoryName,
Instruction = category.Instruction
};
var childCategories = category.Children;
foreach (var childCategory in childCategories)
{
var childCategoryMenuItem = Map(childCategory);
categoryMenuItem.AddChildItem(childCategoryMenuItem);
}
return categoryMenuItem;
}
view
#model IEnumerable<CategoriesViewModel>
#foreach (var item in Model)
{
if (item.ChildItems.Any())
{
<li>
<span>#item.CategoryName</span>
<ul class="dropdown">
<li>
#foreach (var childItem in item.ChildItems)
{
<span>#childItem.CategoryName</span>
}
</li>
</ul>
</li>
}
else
{
<li><span>#item.CategoryName</span></li>
}
}
I have a problem. I cannot save cards without duplicates.
I have 3 tables a Deck, Card table and a CardDeck table which is the junction table.
The goal is to store a deck in the database without the cards being duplicated.
Table Card
namespace MTG_Deck.Models
{
public class Card
{
[Key]
public int CardId { get; set; }
public string Name { get; set; }
public string Text { get; set; }
public string Type { get; set; }
public string SetName { get; set; }
public string Artist { get; set; }
public string ManaCost { get; set; }
public string Rarity { get; set; }
public string ImageUrl { get; set; }
public string Toughness { get; set; }
public string Power { get; set; }
public string id { get; set; }
public virtual ICollection<DeckCard> Decks { get; set; }
}
}
Table Deck
namespace MTG_Deck.Models
{
public class Deck
{
[Key]
public int DeckId { get; set; }
[Required]
public string Name { get; set; }
[DataType(DataType.Date)]
public DateTime CreateAt { get; set; }
public User User { get; set; }
public virtual ICollection<DeckCard> Cards { get; set; }
}
}
Table CardDeck
namespace MTG_Deck.Models
{
public class DeckCard
{
public int DeckID { get; set; }
public int CardID { get; set; }
public Deck Deck { get; set; }
public Card Card { get; set; }
}
}
CardDeckRequest contains the content of the request.
namespace MTG_Deck.Models
{
public class CardDeckRequest
{
[Required]
public int UserID { get; set; }
[Required]
public string Token { get; set; }
[Required]
public Deck Deck { get; set; }
[Required]
[MinLength(5, ErrorMessage = "The deck must contain 60 cards.")]
public List<Card> Cards { get; set; }
}
}
Controller
[HttpPost]
public IActionResult Add([FromBody] CardDeckRequest request)
{
User user = _context.User.Where(u => u.Token == request.Token).FirstOrDefault<User>();
if (user == null) {
var error = new string[] {"You have to be connected to create a deck!"};
BadRequest(new { errors = new { success = error } });
}
List<DeckCard> deckList = new List<DeckCard>();
if (ModelState.IsValid) {
request.Deck.User = user;
foreach (var item in request.Cards)
{
Card card = _context.Card.FirstOrDefault(c => c.Name == item.Name);
if (card == null) {
card = item;
Console.WriteLine("coucou");
}
DeckCard deckCard = new DeckCard {
Card = card,
Deck = request.Deck
};
deckList.Add(deckCard);
}
_context.DeckCard.AddRange(deckList);
_context.SaveChanges();
var error = new string[] {"Your deck has been successfully saved."};
return Ok(new { errors = new { success = error } });
}
return BadRequest(ModelState.ToDictionary(
kvp => kvp.Key,
kvp => kvp.Value.Errors.Select(e => e.ErrorMessage).ToArray()
));
}
I have this code -
var add = (from h in db.Hotels
where h.Address.Contains(hotels.Address)
select h).Take(2);
ViewBag.Related = add;
Now, in the View, I want to display the images, so I'm using this code -
<img src="~/img/#item.FirstOrDefault().Image" />
This is giving me this error -
'System.Data.Entity.DynamicProxies.Hotels_D1EE6FD2E11BD1D9436F26FEA6336CFE76F33C59111E2ABC7C1BBE456FF61C23' does not contain a definition for 'FirstOrDefault'
I've tried using 'joins' also but same error occurs. Please help me out in this! :(
My Hotels class -
public class Hotels
{
[ScaffoldColumn(false)]
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Address { get; set; }
[StringLength(8)]
public string PinCode { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public string FilledBy { get; set; }
public DateTime DateAdded { get; set; }
//public int ImageId { get; set; }
public int TotalRooms { get; set; }
public bool Available { get; set; }
public virtual ICollection <Rooms> Rooms { get; set; }
public virtual ICollection <Images> Images { get; set; }
public virtual ICollection<Ameneties> Ameneties { get; set; }
public virtual ICollection <Bookings> Bookings { get; set; }
public virtual ICollection<NearByLocations> Nearby { get; set; }
public virtual ICollection<Ratings> Ratings { get; set; }
public virtual ICollection<RoomType> RoomTypes { get; set; }
public virtual ICollection<CustomerReviews> Reviews { get; set; }
public virtual ICollection<HotelRules> HotelRules { get; set; }
}
My Images class -
public class Images
{
[ScaffoldColumn(false)]
public int id { get; set; }
public string Image { get; set; }
public int? HotelId { get; set; }
public virtual Hotels Hotels { get; set; }
//public ICollection<Hotels> Hotels { get; set; }
}
I have used this type of collections...
This is my Details View Controller code -
public ActionResult Details(int? id)
{
IEnumerable<Images> galleries = (from gallery in db.Images
where gallery.Hotels.Id == id
select gallery);
ViewBag.Images = galleries;
ViewBag.ImgCount = galleries.Count();
IEnumerable<Ameneties> ameneties = (from a in db.Ameneties
where a.Hotels.Id == id
select a);
ViewBag.Ameneties = ameneties;
IQueryable<Rooms> rooms = (from room in db.Rooms
where room.Hotels.Id == id
select room);
var ratings = (from rating in db.Ratings
where rating.Hotels.Id == id
select rating.Points);
ViewBag.Ratings = ratings;
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Hotels hotels = db.Hotels.Find(id);
if (hotels == null)
{
return HttpNotFound();
}
var add = db.Hotels.Include("Images").Where(h => h.Address.Contains(hotels.Address))
.Select(h => h)
.Take(2)
.ToList();
var model = new MyViewModel { Hotels = add };
ViewBag.Reviews = hotels.Reviews;
ViewBag.Ratings = hotels.Ratings;
ViewBag.NearBy = hotels.Nearby;
ViewBag.RoomTypes = hotels.RoomTypes;
ViewBag.Rules = hotels.HotelRules;
return View(hotels);
}
Could you add ToList() at the end of the query?
var add = (from h in db.Hotels
where h.Address.Contains(hotels.Address)
select h).Take(2)
.ToList();
ViewBag.Related = add;
Then you could call item.Images.FirstOrDefault()?.Image.
#foreach (var item in ViewBag.Related)
{
<img src="~/img/#item.Images.FirstOrDefault().Image" />
}
If it still doesn't work, you will need to explicitly load Image when you query Hotel. For example,
public IActionResult Index()
{
var add = db.Hotels
.Include("Images")
.Where(h => h.Address.Contains(hotels.Address))
.Select(h => h)
.Take(2)
.ToList();
var model = new MyViewModel { Hotels = add };
return View(model);
}
View
#model YourNameSpace.Models.MyViewModel
#foreach (var item in Model.Hotels)
{
<img src="~/img/#item.Images.FirstOrDefault().Image" />
}
Model
public class MyViewModel
{
public List<Hotels> Hotels { get; set; }
}
I want to send a list of names contained in a Database using asp.net
These are my two objects:
public class Shop
{
public int ID { get; set; }
public string Country { get; set; }
public List<Item> Items{ get; set; }
}
public class Item
{
public int ID { get; set; }
public string Name { get; set; }
}
I want to set a get controller in order to retrieve a list of items.
I did something like this:
public IEnumerable<Item> Get(int id)
{
var items= new List<Item>();
var shop= new Shop();
using (var systemDB = new ShopsDB())
{
it = systemDB.Shops.Where(s => s.ID == id).FirstOrDefault<Shop>();
items = it.Items;
}
return items;
}
This return <ArrayOfItem i:nil="true"/>.
I want to get the complete list of Items for one shop (e.g. shop with ID=1)
This will return you the list
using (var systemDB = new ShopsDB())
{
lab = systemDB.Shops.Where(s => s.ID == id).ToList();
items = lab.Items;
}
I solved modifying the Item object:
public class Shop
{
public int ID { get; set; }
public string Country { get; set; }
public List<Item> Items{ get; set; }
}
public class Item
{
public int ID { get; set; }
public string Name { get; set; }
public int ShopID { get; set; }
}
And set a query to select the ShopID
This is an ASP.NET MVC4 project with Entity Framework.
I need to use 2 tables in single view
I have 2 tables in my database (Budgets and Products).
My Models
Budget.cs
public class Budget
{
public int Id { get; set; }
public string Title { get; set; }
public ICollection<Product> Products { get; set; }
}
Product.cs
public class Product
{
public int Id { get; set; }
public string Title { get; set; }
public decimal Value { get; set; }
public int BudgetId { get; set; }
}
BpDb
public class BpDb:DbContext
{
public DbSet<Budget> Budgets { get; set; }
public DbSet<Product> Products { get; set; }
}
MasterBP
public class MasterBP
{
public int Id { get; set; }
public string Title { get; set; }
public List<Product> ProdName { get; set; }
public int CountOfValues { get; set; }
}
I need something about Controller to get ProdName (Title from table products)
public class HomeController : Controller
{
BpDb _db = new BpDb();
public ActionResult Index()
{
var model=_db.Budgets
.Select(r=>new MasterBP
{
Id = r.Id,
Title = r.Title,
ProdName = r.Products.Where(s => s.BudgetId == r.Id).ToList(),
CountOfValues = r.Products.Count()
});
return View(model);
}
}
I need a way to get a list of products Title when the BudgetId = ID (1 Budget can have more than 1 Products)
And my Index is like:
#model IEnumerable<MvcGg.Models.MasterBP>
#foreach (var item in Model)
{
<div>
<h4>#item.Title</h4>
#foreach (var product in item.ProdName)
{
#(product.Title.ToString());
}
Values:#item.CountOfValues
</div>
}
It sounds like (correct me if I am wrong) you want to access two sets of entities in your view model?
Then something along these lines will do:
public class BudgetsViewModel
{
public IEnumerable<MasterBP> MasterBps { get;set; }
public IEnumerable<Product> Products { get;set;}
}
And then you just retrieve all the data and populate the viewmodel and pass it down.
...
var budgets =_db.Budgets
.Select(r=>new MasterBP
{
Id = r.Id,
Title = r.Title,
ProdName = r.Products.Where(s => s.BudgetId == r.Id).ToList(),
CountOfValues = r.Products.Count()
});
var model = new BudgetsViewModel()
{
MasterBps = budgets
//Products here
}
return View(model);
...
However it would be better if you made some form of service layer to handle the retrieving on entities and the mapping to view models instead of having all this logic in your controller.
finally this solve my problem,
Model:
MasterBP
public class MasterBP
{
public int Id { get; set; }
public string Title { get; set; }
public IEnumerable<Product> ProdName { get; set; }
public int CountOfValues { get; set; }
}
I replaced List<> with IEnumerable<>
Controller:
public ActionResult Index()
{
var model=_db.Budgets
.Select(r=>new MasterBP
{
Id = r.Id,
Title = r.Title,
ProdName = r.Products.Where(s => s.BudgetId == r.Id).AsEnumerable(),
CountOfValues = r.Products.Count()
});
return View(model);
}
In addition i have change ToList() in AsEnumerable() and now i can see my result using this
View:
Index
#foreach (var item in Model)
{
<div>
#item.Title
<ul>
#foreach (var prod in item.ProdName)
{
<li>#prod.Title</li>
}
</ul>
<hr />
</div>
}