I have a problem with my program. I have three DB models, hotelcategory, hotelroom type and price. in some categories there are two but in some other there are three categories. we need to make textbox for every room in every category. after that I must post it like this: categoryID, roomÄ°D,price. with for loop l didnt get it. how can l relate these categories to eaach other in arrayList.
public class PrPrice
{
public string Name { get; set; }
public int[] ProgramID { get; set; }
public List<HotelRoomProgram> hotelRoomProgram { get; set; }
public int[] hotelRoomProgramID { get; set; }
public List<HotelCategoryProgram> hotelCategoryPrograms { get; set; }
public int[] hotelCategoryProgramsID { get; set; }
public decimal[] Price { get; set; }
}
for (int i = 0; i < Model.hotelCategoryPrograms.Count(); i++)
{
#Html.HiddenFor(x=>x.hotelCategoryPrograms[i].HotelCategories.ID)
<label>#Model.hotelCategoryPrograms[i].HotelCategories.Name</label>
for (int a =0; a < Model.hotelRoomProgram.Count(); a++)
{
#Html.HiddenFor(x=> x.hotelRoomProgram[a].HotelRoomType.ID)
<label>#Model.hotelRoomProgram[a].HotelRoomType.Name</label>
#Html.TextBoxFor(x => x.Price[a], new {id =a })
}
Related
I am using code first EF 6. So as shown below, I have two related entities. I want it set up in DocumentVersion so that the DocumentId and the DocumentVersionNumber are unique in combination. So there could be lots of entries for DocumentId 1, and lots for DocumentVersionNumber 1, but only one entry can have both DocumentId 1 and DocumentVersionNumber 1. Using the code below, I get an error when creating the migration because of dv.Document.DocumentId in the HasIndex clause. I know it can be done in the database by making the combination unique or even having a complex primary key. I could add a DocumentId to the DocumentVersion object, but I this is my first code first project and am trying to learn from it. Is it possible to achieve what I am wanting without adding the DocumentId solely for that purpose?
public class Document
{
public Document()
{
AllVersions = new HashSet < DocumentVersion >();
History = new HashSet < DocumentHistory >();
}
[ DatabaseGenerated ( DatabaseGeneratedOption.Identity ) ]
public int DocumentId { get; set; }
public string Name { get; set; }
public string Note { get; set; }
public string Description { get; set; }
public CheckoutStatus Status { get; set; } = CheckoutStatus.Locked;
public ICollection < DocumentVersion > AllVersions { get; set; }
public ICollection < DocumentHistory > History { get; set; }
}
public class DocumentVersion
{
public DocumentVersion() => History = new HashSet < DocumentVersionHistory >();
public DocumentVersion ( byte [ ] content )
{
History = new HashSet < DocumentVersionHistory >();
Content = content;
}
public int DocumentVersionId { get; set; }
public int DocumentVersionNumber {get; set; }
public string Note { get; set; }
public PublishingStatus Status { get; set; }
public byte [ ] Content { get; set; }
public Document Document { get; set; }
public ICollection < DocumentVersionHistory > History { get; set; }
}
protected override void OnModelCreating ( DbModelBuilder modelBuilder )
{
modelBuilder.Entity < Document >().HasKey ( d => new { d.DocumentId } );
modelBuilder.Entity < DocumentVersion >().HasKey ( dv => dv.DocumentVersionId ).HasIndex ( dv => new { dv.Document.DocumentId, dv.DocumentVersionNumber } );
...
}
I have five tables in database whose Entity classes are as follows -
Product
public int ProductId { get; set; }
public string Name { get; set; }
public int Category { get; set; }
public string Description { get; set; }
public string Brand { get; set; }
public virtual ProductCategory ProductCategory { get; set; }
public virtual ICollection<ProductImage> ProductImages { get; set; }
public virtual ICollection<ProductVariantMapping> ProductVariantMappings
ProductCategory
public int CategoryId { get; set; }
public string Name { get; set; }
public virtual ICollection<Product> Products { get; set; }
ProductImage
public int ProductImageId { get; set; }
public int ProductId { get; set; }
public byte[] Image { get; set; }
public virtual Product Product { get; set; }
ProductVariantMapping
public int MappingId { get; set; }
public int ProductId { get; set; }
public int ProductVariantId { get; set; }
public string Value { get; set; }
public System.Guid GUID { get; set; }
public virtual Product Product { get; set; }
public virtual ProductVariant ProductVariant { get; set; }
ProductVariant
public int ProductVariantId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<ProductVariantMapping> ProductVariantMappings
I want to get Product Details which should include ProductId, ProductName, Category, Description, Brand, Image(Only 1 for now), and Variants*
*Variants would be a list of all the variants of a product. A single variant can be a combination of all the VariantIds with same GUIDs. (VariantName is in ProductVariant table and VariantValue is in ProductVariantMapping table and Price is in inventory table).
So, I used method-based linq for this purpose.
EkartEntities ekartEntities = new EkartEntities();
var productDetails = ekartEntities.Products.Include(p =>
p.ProductVariantMappings).Include(p => p.ProductImages).Include(p =>
p.ProductCategory).Where(p => p.ProductId ==
productDetailDTO.ProductId).ToList();
Now I have to convert my product into a ProductDetailDTO.
ProductDetailDTO
public class ProductDetailDTO
{
public int ProductId { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public byte[] Image { get; set; }
public string Description { get; set; }
public string Brand { get; set; }
public List<Variant> Variants { get; set; }
}
public class Variant
{
public string Name { get; set; }
public string Value { get; set; }
public System.Guid Guid { get; set; }
public decimal Price { get; set; }
}
I started doing this like this -
void ToDTO(List<Product> products)
{
EkartEntities ekartEntities = new EkartEntities();
ProductDetailDTO productDetailDTO = new ProductDetailDTO();
foreach (var item in products)
{
productDetailDTO.ProductId = item.ProductId;
productDetailDTO.Name = item.Name;
productDetailDTO.Category = item.ProductCategory.Name;
productDetailDTO.Description = item.Description;
productDetailDTO.Brand = item.Brand;
productDetailDTO.Image = item.ProductImages.ElementAt(0).Image;
foreach (var variant in item.ProductVariantMappings)
{
productDetailDTO.Variants = variant.ProductVariant // ?
}
}
}
I don't know how do I proceed further. How can I extract the variant based on the GUIDs?
The logic of combining of ProductVariant entries with same GUID in mapping table doesn't seem clear from the question, however you can group entries in ProductVariantMappings by GUID and then add any logc you like on group. Here is an example where I take first name and value in a groub of variant with the same GUID:
void ToDTO(List<Product> products)
{
EkartEntities ekartEntities = new EkartEntities();
ProductDetailDTO productDetailDTO = new ProductDetailDTO();
foreach (var item in products)
{
productDetailDTO.ProductId = item.ProductId;
productDetailDTO.Name = item.Name;
productDetailDTO.Category = item.ProductCategory.Name;
productDetailDTO.Description = item.Description;
productDetailDTO.Brand = item.Brand;
productDetailDTO.Image = item.ProductImages.ElementAt(0).Image;
productDetailDTO.Variants = item.ProductVariantMappings
.GroupBy(pm => pm.GUID)
.Select(g => new Variant
{
Guid = g.Key,
// Here should be some logic for getting a name of the combination of Variants
// I just take first
Name = g.FirstOrDefault()?.ProductVariant?.Name,
// Here should be some logic for getting a value of the combination of Variants
// Take first again
Value = g.FirstOrDefault()?.Value,
Price = // need inventory table to compute price
})
.ToList();
}
}
Also note that you need somehow add relation to inventory table, which is not presented in question. Hope it helps.
I have 3 entities: Surveys, Batch, and Removedwhere Surveys is the parent table and the other two are related to Surveys with foreign keys.
public class Surveys
{
public int TireID { get; set; }
public dateTime DateCreated{ get; set; }
}
public class Batch
{
public int TireID { get; set; }
public int TirePosition{ get; set; }
public ICollection<Surveys> Survey{ get; set; }
}
public class Removed
{
public int TireID { get; set; }
public int TirePosition{ get; set; }
public dateTime DateRemoved{ get; set; }
public ICollection<Surveys> Survey{ get; set; }
}
I have a query to get the Batch of tires that formed part of the Survey, and the removed ones from Removed:
var surveys = ctx.Surveys.Where(a => a.DateSubmitted >= startDate && a.DateSubmitted < endDate).Select(a => new T_Batch
{
//don't know how to get desired result
}).ToList();
I have a custom type that the query result (T_Batch) should be of:
public class T_Batch
{
public int TireID { get; set; }
public int TirePosition { get;
public T_Removed Removed{ get; set; }
}
public class T_Removed
{
public int TireID { get; set; }
public int TirePosition { get; set; }
}
The query result brings back a list of Surveys which is fine, but I would like a list of tires per Survey list item, and a removed tire (if there was one removed) per tire position. I don't know how to achieve this.
Example of desired result:
0 Batch_Tire1
1 Batch_Tire2
0 Removed_Tire
2 Batch_Tire3
3 Batch_Tire4
0 Removed_Tire
etc...
UPDATE
I could do something like this, but I need to get the result set as my custom type.
var surveys = ctx.TM_Survey.Where(a => a.DateSubmitted >= startDate && a.DateSubmitted < endDate).Select(a => new
{
BatchItem = a.Batch,
RemovedItem = a.Removed
}).ToList();
UPDATE 2
Or I could do this, but the result only returns a limited number of rows equal to the number of keys in TM_Survey
var surveys = ctx.TM_Survey.Where(a => a.DateSubmitted >= startDate && a.DateSubmitted < endDate).Select(a => new T_Batch
{
TireID = a.T_Batch.Select(b => b.ID).First(),
TirePosition = a.T_Batch.Select(b => b.TirePosition).First()
Removed = new T_Removed
{
//...etc
}
}).ToList();
I have Models created through Entity Framework as:
public partial class Order
{
public Order()
{
this.OrderDetails = new HashSet<OrderDetail>();
}
public int OrderID { get; set; }
public string OrderNo { get; set; }
public System.DateTime OrderDate { get; set; }
public string Description { get; set; }
public virtual ICollection<OrderDetail> OrderDetails { get; set; }
}
Then another Details Table:
public partial class OrderDetail
{
public int OrderItemsID { get; set; }
public int OrderID { get; set; }
public string ItemName { get; set; }
public int Quantity { get; set; }
public decimal Rate { get; set; }
public decimal TotalAmount { get; set; }
public virtual Order Order { get; set; }
}
To See the Master Detail Data I made MasterDetails model As:
public class OrderVM
{
public string OrderNo { get; set; }
public DateTime OrderDate { get; set; }
public string Description { get; set; }
public List<OrderDetail> OrderDetails {get;set;}
}
I'm trying to make a method that return a LIST with Join query results but
I'm receiving #Anonymous type error here is my Code:
public static List<OrderVM > mylist()
{
List<OrderVM> slist = new List<OrderVM>();
using (MyDatabaseEntities1 dc = new MyDatabaseEntities1())
{
var myvalues = from O in dc.Orders
join D in dc.OrderDetails
on
O.OrderID equals D.OrderID
select new
{
O.OrderID,
O.OrderDate,
D.Quantity,
D.Rate
};
foreach(var myorders in myvalues)
{
slist.Add(myorders);
}
return slist;
}
}
I need a help that how I can I create a generic list with database fields
Create new class:
public class OrderDetailsModel
{
public int OrderId { get; set; }
public DateTime OrderDate { get; set; }
public int Quantity { get; set; }
public decimal Rate { get; set; }
}
and return list of objects of this class from your method:
using (MyDatabaseEntities1 dc = new MyDatabaseEntities1())
{
var myvalues = from O in dc.Orders
join D in dc.OrderDetails
on
O.OrderID equals D.OrderID
select new OrderDetailsModel
{
OrderId = O.OrderID,
OrderDate = O.OrderDate,
Quantity = D.Quantity,
Rate = D.Rate
};
return myvalues.ToList();
}
Database Models of my Application are:
public class Restaurant
{
public int Id { get; set; }
.........
}
public class Review
{
public int Id { get; set; }
public string ReviewTitle { get; set; }
public string ReviewContent { get; set; }
public int UserId { get; set; }
public int RestaurantId { get; set; }
}
public ReviewHelpful
{
public int Id { get; set; }
public int ReviewId { get; set; }
public bool IsHelpfull { get; set; }
}
public ReviewImage
{
public int Id { get; set; }
public string ImageLink { get; set; }
public int ReviewId { get; set; }
}
There is no navigation property in any table. In ReviewHelpful table, If user finds helpfull of this review than value is true otherwise false.
Now I want to create a view-model Like this:
public class ReviewViewModel
{
public int ReviewId { get; set; }
public int RestaurantId { get; set; }
public string ReviewTitle { get; set; }
public string ReviewContent { get; set; }
public int UserId { get; set; }
public int NumberOfHelpfull { get; set; }
public int NumberOfNotHelpfull { get; set; }
public List<string> ImagesLinks { get; set; }
}
For that reason, I want to write this kind of query :
var reviews = (from review in _foodTrackerContext.RestaurantReviews
join helpful in _foodTrackerContext.Helpfuls on review.Id equals helpful.ReviewId
join reviewPicture in _foodTrackerContext.ReviewPictures on review.Id equals reviewPicture.ReviewId
where review.ResturantId == 2
select new ReviewViewModel()
{
Id = review.Id,
RestaurantId = 2,
ReviewTitle = review.ReviewTitle,
ReviewContent = review.ReviewContent,
NumberOfHelpfull = .. ??,
NumberOfNotHelpfull = ... ??,
ImagesLinks = ... ???
}
I can not retrieve HelpfulYes, HelpfulNo, ImagesLinks with this query. What would be query for finding these variables?.
This query produces multiple rows for single review with each ReviewImage and each ReviewHelpful.
The query that ypu need to do is this one:
var model =
from review in ctx.Reviews
where review.RestaurantId == 2
join helpful in ctx.ReviewHelpfuls
on review.Id equals helpful.ReviewId into helpfuls
join image in ctx.ReviewImages
on review.Id equals image.ReviewId into images
select new RestaurantReviewViewModel
{
Id = review.Id,
RestaurantId = 2,
ReviewTitle = review.ReviewTitle,
ReviewContent = review.ReviewContent,
NumberOfHelpfull = helpfuls.Count(h => h.IsHelpfull),
NumberOfNotHelpfull = helpfuls.Count(h => !h.IsHelpfull),
ImagesLinks = (from image in images select image.ImageLink).ToList()
};
Please, note that when you do a one to manyh join you need to include an into to give a nameto the joined entities to be able to work on them.
I've used the dot syntax for selecting the count, but you could use the query syntax if you wanted. Over time, I've found dot synatx more natural.
NOTE: if you used navigation properties this would become much easier. Why are you not using them? With navigation properties you don't need to make the joins explicitly, as they are already available.
List<ReviewViewModel> listModel = new List<ReviewViewModel>();
context.dbRestaurant
.include("Review")
.include("Review.ReviewHelpful")
.include("Review.ReviewImage").ToList().ForEach((item) =>
{
ReviewViewModel model = new ReviewViewModel();
model.ID = item.ID
listModel.Add(model);
});