EnumerableExtensions.Join inaccessible due to its protection level - join

am getting a Enumerable Extensions join is inaccessible due to its protection level error. I am using EF Core for database connections. How to perform joins using LINQ ??
var seatBookings =(
from sb in Context.SeatBookings
join bd in Context.BookingDetails on sb.BookingId
equals bd.Id
where bd.ShowId == showId
select sb
).ToList();
public partial class SeatBookings
{
public int Id { get; set; }
public int? BookingId { get; set; }
public int SeatNumber { get; set; }
public BookingDetails Booking { get; set; }
}
public partial class BookingDetails
{
public BookingDetails()
{
SeatBookings = new HashSet<SeatBookings>();
}
public int Id { get; set; }
public int ShowId { get; set; }
public string Email { get; set; }
public string MobileNumber { get; set; }
public int TheaterId { get; set; }
public int BookedSeatsCount { get; set; }
public ShowDetails Show { get; set; }
public ICollection<SeatBookings> SeatBookings { get; set; }
}

Related

how to select from client model all clients thats make an order orderby Count of purchase

I want to select from client model all client without duplicate client and order them by count of the purchases they already did
This is model of client :
public class Client
{
public int ID { get; set; }
public string FacebookName { get; set; }
public string RealName { get; set; }
public string Mobile { get; set; }
public int RegionID { get; set; }
public string Addressdetails { get; set; }
public string Email { get; set; }
[DataType(DataType.Date)]
public DateTime DateOfStart { get; set; }
public virtual Region Region { get; set; }
public virtual List<Order> order { get; set; }
}
this is Order model :
public class Order
{
public int ID { get; set; }
public int ClientID { get; set; }
public Nullable<int> ShippingID { get; set; }
public Nullable<int> SharepointID { get; set; }
public Nullable<int> CallStatuID { get; set; }
public Nullable<int> ShippingStatuID { get; set; }
public Nullable<int> ShippingStatuReasonsID { get; set; }
[DataType(DataType.Date)]
public DateTime Date { get; set; }
// public int OrderStatusID { get; set; }
public Nullable<decimal> ShippingCost { get; set; }
public string purchasetype { get; set; }
public string Notes { get; set; }
public Nullable<decimal> Discount { get; set; }
public decimal Total { get; set; }
[DefaultValue(false)]
public bool Prepared { get; set; }
[DefaultValue(false)]
public bool Done { get; set; }
[DefaultValue(false)]
public bool Shipe { get; set; }
[DefaultValue(false)]
public bool Collected { get; set; }
[DataType(DataType.Date)]
public Nullable<DateTime> DateOfCall { get; set; }
[DataType(DataType.Date)]
public Nullable<DateTime> StockCheckDate { get; set; }
[DataType(DataType.Date)]
public Nullable<DateTime> ShippingDate { get; set; }
public virtual List<OrderDetail> orderdetails { get; set; }
public virtual Client Client { get; set; }
public virtual Shipping shipping { get; set; }
public virtual Sharepoint sharepoint { get; set; }
public virtual CallStatu callStatu { get; set; }
public virtual ShippingStatu ShippingStatus { get; set; }
public virtual ShippingStatuReason ShippingStatuReasons { get; set; }
}
and finally this is OrderDetails model:
public class OrderDetail
{
public int ID { get; set; }
public int OrderID { get; set; }
public Nullable<int> ItemID { get; set; }
public Nullable<int> SizeID { get; set; }
public int ColorID { get; set; }
public decimal Price { get; set; }
public Nullable<int> StockStatuID { get; set; }
public Nullable<int> StockStatuReasonsID { get; set; }
public int Quantity { get; set; }
public decimal Total { get; set; }
public virtual Order order { get; set; }
public virtual Item item { get; set; }
public virtual Size size { get; set; }
public virtual Colors Colors { get; set; }
public virtual StockStatu stockStatus { get; set; }
public virtual StockStatuReason StockStatuReasons { get; set; }
}
I tried this way but it the select is repeat Clients If they make more than one purchase:
public JsonResult TopClientCount()
{
var index = (from dex in db.Clients
join O in db.Orders on dex.ID equals O.ClientID
select new
{
RealName = dex.RealName,
FacebookName = dex.FacebookName,
GovernorateName = dex.Region.governorate.GovernorateName,
RegionName = dex.Region.RegionName,
OrderCount = O.orderdetails.Count()
}).AsEnumerable().OrderByDescending(o => o.OrderCount)
.Distinct()
.ToList();
return Json(index, JsonRequestBehavior.AllowGet);
}
You can use let statement and you can use navigation properties i.e (order ,orderdetails) instead of joins
var index = (from dex in db.Clients
//This will give you all the order details for a client
let orderDetails = dex.order.SelectMany(p=> p.orderdetails)
select new
{
RealName = dex.RealName,
FacebookName = dex.FacebookName,
GovernorateName = dex.Region.governorate.GovernorateName,
RegionName = dex.Region.RegionName,
OrderCount = orderDetails.Count()
}).AsEnumerable().OrderByDescending(o => o.OrderCount)
.Distinct()
.ToList();

Eager loading with include method does not return data from collection

I wanted to eager load a collection but it does not returns any data
I have tried nothing
//here is my controller
var accountGroup = await db.AccountGroups.Include(a=>a.AccountGroupTypes)
.Include(a=>a.AccountPrimaryGroups).ToListAsync();
//here is my model
public int? Id { get; set; }
public string Name { get; set; }
public int AccountGroupTypeId { get; set; }
public ICollection<AccountGroupType> AccountGroupTypes { get; set; }
public int AccountPrimaryGroupId { get; set; }
public ICollection<AccountPrimaryGroup> AccountPrimaryGroups { get; set; }
public DateTime CreationDateUtcNow{ get; set; }
It returns AccountGroupData but It does not return AccountGroupTypes and AccountPrimaryGroup data
Mark your AccountGroupTypes and AccountPrimaryGroups as virtual properties and be sure that you have a parameterless constructor which is initializing those properties.
public int? Id { get; set; }
public string Name { get; set; }
public int AccountGroupTypeId { get; set; }
public vitual ICollection<AccountGroupType> AccountGroupTypes { get; set; }
public int AccountPrimaryGroupId { get; set; }
public virtual ICollection<AccountPrimaryGroup> AccountPrimaryGroups { get; set; }
public DateTime CreationDateUtcNow{ get; set; }
public AccountGroup()
{
this.AccountGroupTypes = new HashSet<AccountGroupType>();
this.AccountPrimaryGroups = new HashSet<AccountPrimaryGroups>();
}

Column name 'ApplicationUserId' in table 'Tickets' is specified more than once

DB relationship:
1ApplicationUser : n Tickets
1ApplicationUser: n Activities
1Tickets:n Activites
I would like to design above database using code first. However,there's error warning
"Column name 'ApplicationUserId' in table 'Tickets' is specified more
than once."
However, I checked there's only one ApplicationUserId in Table Ticket as shown in below class.
Would anyone please help how to fix it? Thanks.
The entity class are as following:
Ticket Class
public class Ticket
{
public int ID { get; set; }
public Ticket()
{ TicketCreateDate = DateTime.Now; }
public DateTime TicketCreateDate { get; set; }
public int TicketCreateBy { get; set; }
public DateTime TicketCloseDate { get; set; }
public int TicketCloseBy { get; set; }
public DateTime LastTicketUpdateDate { get; set; }
public int LastUpdateBy { get; set; }//Ticket Last Update by
public DateTime TicketAssignedDate { get; set; }
public int TicketAssignedTo { get; set; }
public string TicketType { get; set; }
public string Priority { get; set; }// Ticket priority
public string TicketStatus { get; set; }
public string TicketDescription { get; set; }
public int DeviceUserID { get; set; }
public string DeviceBrand { get; set; }
public string DeviceType { get; set; }
public virtual ICollection<Activity> Activities { get; set; }
public int ApplicationUserID { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
}
Acitivity Class
public class Activity
{
public int ID { get; set; }
public Activity()
{
CreatedTime = DateTime.Now;
}
public DateTime CreatedTime { get; set; }
public string ActivityDetails { get; set; }
public int ApplicationUserID { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
public int TicketId { get; set; }
public virtual Ticket Ticket { get; set; }
}
ApplicationUser Class
public class ApplicationUser : IdentityUser
{
public virtual ICollection<Ticket> Tickets { get; set; }
public virtual ICollection<Activity> Activities { get; set; }
}

Many to Many Relationship in MVC 5 EF6

Im using MVC 5 and EF 6.
There are 3 tables in my code: "Leads", "Phones", "LeadStatus" and a junction table "LeadPhones".
I want to show following properties in Index View:
Leads.FullName
LeadStatus.Status
Phones.PhoneID
How should I configure the LeadController?
public partial class Leads
{
public Leads()
{
this.LeadPhones = new HashSet<LeadPhones>();
}
[Key]
public int LeadID { get; set; }
public string Title { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName { get { return NamePrefix + " " + FirstName + " " + LastName; } }
public int LeadStatusID { get; set; }
public virtual LeadStatus LeadStatus { get; set; }
public virtual ICollection<LeadPhones> LeadPhones { get; set; }
}
and
public partial class Phones
{
public Phones()
{
this.AccountPhones = new HashSet<AccountPhones>();
this.ContactPhones = new HashSet<ContactPhones>();
this.EmployeePhones = new HashSet<EmployeePhones>();
this.LeadPhones = new HashSet<LeadPhones>();
}
[Key]
public int PhoneID { get; set; }
public string PhoneNo { get; set; }
public virtual ICollection<LeadPhones> LeadPhones { get; set; }
}
and
public partial class LeadPhones
{
[Key]
public int LeadPhoneID { get; set; }
public int LeadID { get; set; }
public int PhoneID { get; set; }
public virtual Leads Leads { get; set; }
public virtual Phones Phones { get; set; }
}
and
public partial class LeadStatus
{
public LeadStatus()
{
this.Leads = new HashSet<Leads>();
}
[Key]
public int LeadStatusID { get; set; }
public string Status { get; set; }
public virtual ICollection<Leads> Leads { get; set; }
}
the following does not work:
public ActionResult Index()
{
var leads = db.Leads.Include(l => l.LeadStatus).Include(l => l.LeadPhones);
return View(leads);
}
The main problem is the "PhoneNo". How could I get that from a junction table?
Remove the LeadPhoneID column from the LeadPhones table -- It isn't necessary and will only cause problems for you. Remove all references to LeadPhones in your classes. Leads a virtual property to Phones (not LeadPhones). LeadPhones has a virtual property to Leads (not LeadPhones).
After you do that, then you should be able to:
var leads = db.Leads.Include(l => l.LeadStatus).Include(l => l.Phones);
public partial class Lead
{
public Lead()
{
this.Phones = new HashSet<Phone>();
}
[Key]
public int LeadID { get; set; }
public string Title { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName { get { return NamePrefix + " " + FirstName + " " + LastName; } }
public int LeadStatusID { get; set; }
public virtual LeadStatus LeadStatus { get; set; }
public virtual ICollection<Phone> Phones { get; set; }
}
public partial class Phone
{
public Phone()
{
this.AccountPhones = new HashSet<AccountPhones>();
this.ContactPhones = new HashSet<ContactPhones>();
this.EmployeePhones = new HashSet<EmployeePhones>();
this.Leads = new HashSet<Lead>();
}
[Key]
public int PhoneID { get; set; }
public string PhoneNo { get; set; }
public virtual ICollection<Lead> Leads { get; set; }
}
public partial class LeadStatus
{
public LeadStatus()
{
this.Leads = new HashSet<Lead>();
}
[Key]
public int LeadStatusID { get; set; }
public string Status { get; set; }
public virtual ICollection<Lead> Leads { get; set; }
}
I've also corrected the plurality of your classes. Lead vs Leads. The class describes a single lead, so it should be called Lead not Leads. Phone vs Phones.

Retrieve specific record from database in MVC

Please look at my sample code below:
public class LoginModel
{
[Key]
public int LoginId { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
}
public class RegisterModel
{
[Key]
public int RegisterId { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public string Email { get; set; }
}
public class Cart
{
[Key]
public int RecordId { get; set; }
public string CartId { get; set; }
public int ProductId { get; set; }
public int Count { get; set; }
}
There are many records in the cart table added by many users.
But i want to show the own record of a user from the cart table if he is logged in..how can I do this??
You have to insert a foreign-key property to your Cart class:
public class UserModel
{
[Key]
public int LoginId { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
}
public class Cart
{
[Key]
public int RecordId { get; set; }
public string CartId { get; set; }
public int ProductId { get; set; }
public int Count { get; set; }
public virtual UserModel User { get; set; } // <-- added
}
Then you can use LINQ to query the Cart-items for a specific user:
var cartItems = dbContext.Cart.Where(a => a.User == loggedInUser);
var query = from a in db.Pages
where a.title.Contains(myTitle)
select a;
var item=query.FirstOrDefault();
if(item!=null)
return View(item);
else
return View("NotFound");

Resources