I am using Microsoft SQL Server Managment Studio 2008. I am trying to get information from database. There are the example of linq query
var fbPost = db.FacebookStatusUpdates
.Where(f => f.Status == FacebookNotificationStatus.Active &&
f.Alarm.User.FbStatus == true)
.AsEnumerable()
.Where(f => f.FacebookUpdateTime - f.ClientTime.Offset <=
DateTimeOffset.Now.UtcDateTime.AddSeconds(f.Offset))
.ToList();
There is my stored procedure:
declare #date2 datetimeoffset=getutcDate();
select a.*
from [Alllarm].[dbo].[FacebookStatusUpdates] a
inner join [Alllarm].[dbo].[Alarms] b
inner join [Alllarm].[dbo].[Users] o
on o.[Id] = b.[User_id] on b.Id=a.[Alarm_id]
where o.[FbStatus] = 1 and a.Status=2
and DATEADD(hour,datepart(tz,a.ClientTime),a.FacebookUpdateTime)<=DATEADD(second, a.Offset, #date2);
Thare is my model:
public class User
{
public long Id { get; set; }
public string FacebookID { get; set; }
public string AccessToken { get; set; }
public DateTime UpdateTime { get; set; }
public bool? FbStatus { get; set; }
public virtual Device Device { get; set; }
public virtual List<Alarm> Alarms { get; set; }
public virtual List<Sms> Sms { get; set; }
}
public class Alarm
{
public long Id { get; set; }
public DateTime StartDate { get; set; }
public int Snoozes { get; set; }
public bool Repeat { get; set; }
public DateTime AlarmUpdateTime { get; set; }
public virtual User User { get; set; }
public virtual List<FacebookNotificationStatus> StatusUpdates { get; set; }
}
public class FacebookStatusUpdate
{
public long Id { get; set; }
public DateTime FacebookUpdateTime { get; set; }
public string PostId { get; set; }
public DateTime? FacebookPostTime { get; set; }
public DateTimeOffset ClientTime { get; set; }
public int Offset { get; set; }
public virtual FacebookNotificationStatus Status { get; set; }
public virtual Alarm Alarm { get; set; }
}
But when I ran a stored procedure nothing heppend. I think that I miss something in that line
and DATEADD(hour,datepart(tz,a.ClientTime),a.FacebookUpdateTime)<=DATEADD(second, a.Offset, #date2);
Can somebody help me ?
a.ClientTime has type DateTimeOffset 2013-11-13 12:03:36.0000000 +02:00
a.Offset type int (seconds)
Probem is there:
DATEADD(hour,datepart(tz,a.ClientTime),a.FacebookUpdateTime)
datepart(tz,a.ClientTime) return offset in minutes, so I change to
DATEADD(minute,datepart(tz,a.ClientTime),a.FacebookUpdateTime)
2013-11-13 12:03:36.0000000 +02:00 datepart return 120.
Related
I have 2 tables SaleMaster
public class SaleMaster
{
public int Id { get; set; }
public List<Installments> Installments { get; set; }
}
public class Installments
{
public int Id { get; set; }
public decimal Amount { get; set; }
public DateTime Month { get; set; }
public bool IsPaid { get; set; }
}
Now I want to get the all the Installments with pending due dates,
but I also want to include the SaleMaster ID from Installment table so I can navigate back.
List<Installments> instalment = con.DbSetInstallments
.Where(x => x.Month < d && x.IsPaid == false)
.ToList();
Now I want to take SaleMaster from this list of which Installments are due.
You can add a relational property to SaleMaster in Installments:
public class Installments
{
public int Id { get; set; }
public decimal Amount { get; set; }
public DateTime Month { get; set; }
public bool IsPaid { get; set; }
public int SaleMasterId { get; set; }
public virtual SaleMaster SaleMaster { get; set; }
}
This way you have easy access to the SaleMaster's Id.
I have two table like this -
public class Job
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime AddedTime { get; set; } = DateTime.Now;
public DateTime LastEdit { get; set; } = DateTime.Now;
public string Explanation { get; set; }
public string PhotoString { get; set; }
public bool isActive { get; set; } = true;
public int CompanyId { get; set; }
public Company Company { get; set; }
}
and company -
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Explanation { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
public string PhotoString { get; set; }
public bool isActive { get; set; } = true;
public int AppUserId { get; set; }
public AppUser AppUser { get; set; }
public List<Job> Jobs { get; set; }
}
I only want to get AppUserId from Company and all Jobs from every Company. I tried this and it gave me error.
using var context = new SocialWorldDbContext();
return await context.Jobs.Where(I => I.isActive == true && I.Company.isActive).Include(I=>I.Company.AppUserId).ToListAsync();
So my question is there any way I can get this data from parent?
Include adds whole entities to the output. To add just one property use Select, something like
context.Jobs
.Where(I => I.isActive == true && I.Company.isActive)
.Select(e => new {Job=e, CompanyAppUserId = e.Company.AppUserId})
.ToListAsync();
What i am essentially trying to do is get all of the packages that have NOT been assigned a package location price for a specific location...
I have the following SQL:
SELECT * FROM Package
LEFT JOIN PackageLocationPrices ON Package.Id = PackageLocationPrices.PackageId
Where PackageLocationPrices.LocationId IS NULL
How can i convert this into Linq to entities?
I have tried something like this:
this.db.Packages.Include(p => p.PackageLocationPrices).Where(p => p.Id == p.PackageLocationPrices.????).ToList();
I am able to join package location prices but i am unable to get the properties of the packagelocationprices to do the SQL above? Below is my schema...The PackageLocationPrices.PackageId is a foreign key of Package.Id
Package Entitiy:
public partial class Package
{
public Package()
{
this.DiscountCodes = new HashSet<DiscountCode>();
this.PackageLocationPrices = new HashSet<PackageLocationPrice>();
this.Memberships = new HashSet<Membership>();
}
public int Id { get; set; }
public string Name { get; set; }
public int PackageOrder { get; set; }
public int PackageTypeId { get; set; }
public int PackagePeriodDays { get; set; }
public int PackagePeriodMonths { get; set; }
public int PackageSuspensionLimit { get; set; }
public int PackageSuspensionLimitIfAdminOverride { get; set; }
public int PackageSuspensionMinLength { get; set; }
public int PackageSuspensionMaxLength { get; set; }
public int PackageSuspensionsMaxLengthCombined { get; set; }
public int PackagePaymentHolidayLimit { get; set; }
public int PackagePaymentHolidayMinLength { get; set; }
public int PackagePaymentHolidayMaxLength { get; set; }
public int PackageVisitLimit { get; set; }
public bool PackageIsActive { get; set; }
public bool PackageIsReoccuring { get; set; }
public bool PackagePayInFull { get; set; }
public bool PackageIsSession { get; set; }
public System.DateTime CreatedDate { get; set; }
public System.DateTime ModifiedDate { get; set; }
public string CreatedBy { get; set; }
public string ModifiedBy { get; set; }
public virtual AspNetUser AspNetUserCreatedBy { get; set; }
public virtual AspNetUser AspNetUserModifiedBy { get; set; }
public virtual ICollection<DiscountCode> DiscountCodes { get; set; }
public virtual PackageType PackageType { get; set; }
public virtual ICollection<PackageLocationPrice> PackageLocationPrices { get; set; }
public virtual ICollection<Membership> Memberships { get; set; }
}
Package Location Price Entity:
public partial class PackageLocationPrice
{
public int Id { get; set; }
public int LocationId { get; set; }
public int PackageId { get; set; }
public decimal MonthlyPrice { get; set; }
public decimal TotalPrice { get; set; }
public System.DateTime CreatedDate { get; set; }
public System.DateTime ModifiedDate { get; set; }
public string CreatedBy { get; set; }
public string ModifiedBy { get; set; }
public virtual AspNetUser AspNetUserCreatedBy { get; set; }
public virtual AspNetUser AspNetUserModifiedBy { get; set; }
public virtual Location Location { get; set; }
public virtual Package Package { get; set; }
}
var result = (from p in Package
join q in PackageLocationPrices on p.Id equals q.PackageId into pq
from r in pq.DefaultIfEmpty()
select new {p, r}).ToList();
This should return something exactly like your SQL query.
I think you can create your query from another perspective:
var query=(from pl in db.PackageLocationPrices
where pl.LocationId == null
select pl.Package).ToList();
If you have disabled lazy loading then also need to use the Include extension method:
var query=(from pl in db.PackageLocationPrices.Include(p=>p.Package)
where pl.LocationId == null
select pl.Package).ToList();
Using method syntax would be this way:
var query=db.PackageLocationPrices.Include(p=>p.Package)
.Where(pl=>pl.LocationId == null)
.Select(pl=>pl.Package)
.ToList();
If you want as result both Package and PackageLocationPrice, then do this:
var query=db.PackageLocationPrices.Include(p=>p.Package)
.Where(pl=>pl.LocationId == null)
.ToList();
With this last query your are going to get a list of PackageLocationPrice, and if you want see the related Package for a givenPackageLocationPrice, you can use the Package navigation property.
I have a linq query below that can't find 'DateTimeScheduled'
var yogaSpace = (from u in context.YogaSpaces
orderby u.Address.LocationPoints.Distance(myLocation)
where ((u.Address.LocationPoints.Distance(myLocation) <= 8047) && (u.Events.DateTimeScheduled >= classDate))
select u).ToPagedList(page, 10);
DateTimeScheduled is red and intellisense can't find anything inside u.Events almost like it doesn't exist. Intellisnse doesn't see the members inside Events.
Here is my YogaSpace and YogaSpaceEvent objects. I can compile everything fine if I remove the clause "&& (u.Events.DateTimeScheduled >= classDate)", furthermore I have data in my table for this object that I seeded to use for testing!
public class YogaSpace
{
public int YogaSpaceId { get; set; }
[Index(IsUnique = false)]
[Required]
[MaxLength(128)]
public string ApplicationUserRefId { get; set; }
public virtual YogaSpaceOverview Overview { get; set; }
public virtual YogaSpaceDetails Details { get; set; }
public virtual ICollection<YogaSpaceImage> Images { get; set; }
[Required]
public ListingComplete ImageCompleted { get; set; }
public byte[] Thumbnail { get; set; }
public virtual YogaSpaceListing Listing { get; set; }
public virtual YogaSpaceAddress Address { get; set; }
public virtual ICollection<YogaSpaceReview> Reviews { get; set; }
[Required]
public DateTime DateCreated { get; set; }
public virtual ICollection<YogaSpaceEvent> Events { get; set; }
[Required]
[Index]
public YogaSpaceStatus Status { get; set; }
[Required]
[Range(0, 4)]
public int StepsToList { get; set; }
[ForeignKey("ApplicationUserRefId")]
public virtual ApplicationUser ApplicationUser { get; set; }
}
public class YogaSpaceEvent
{
public int YogaSpaceEventId { get; set; }
//public string Title { get; set; }
[Index]
//research more about clustered indexes to see if it's really needed here
//[Index(IsClustered = true, IsUnique = false)]
public DateTime DateTimeScheduled { get; set; }
public int AppointmentLength { get; set; }
public int StatusEnum { get; set; }
[Index]
public int YogaSpaceRefId { get; set; }
[ForeignKey("YogaSpaceRefId")]
public virtual YogaSpace YogaSpace { get; set; }
}
The property u.Events is a collection of events. Therefore it cannot have a single value for DateTimeScheduled, it has multiple values.
You need to select events that have DateTimeScheduled >= classDate first. Something like this:
var yogaSpace = (from u in context.YogaSpaces
orderby u.Address.LocationPoints.Distance(myLocation)
where ((u.Address.LocationPoints.Distance(myLocation) <= 8047)
&& (u.Events.Any(e => e.DateTimeScheduled >= classDate)))
select u).ToPagedList(page, 10);
The changed portion of the code u.Events.Any(e => e.DateTimeScheduled >= classDate) will now return a boolean true or false that indicates if any of the events are scheduled on or after the class date.
I have 2 models:
public partial class Movie
{
public Movie()
{
TimeTables = new HashSet<TimeTable>();
}
[Key]
public int MovieId { get; set; }
public string MovieName { get; set; }
public int MovieGenre { get; set; }
public string MoviePicture { get; set; }
public string MovieDescription { get; set; }
public string MovieShortText { get; set; }
public bool? MovieIs3d { get; set; }
public bool? MovieIsImax { get; set; }
public int MovieLanguage { get; set; }
public bool? MovieSubtitled { get; set; }
public int? MovieMinimalAge { get; set; }
public bool? MovieHasDrugs { get; set; }
public bool? MovieHasViolence { get; set; }
public bool? MovieHasSex { get; set; }
public bool? MovieHasSwearing { get; set; }
public bool? MovieIsScary { get; set; }
public bool? MovieHasDiscrimination { get; set; }
public string MovieTrailer { get; set; }
public int MovieLength { get; set; }
public int? Genre_GenreId { get; set; }
public int? Language_LanguageId { get; set; }
public virtual Genre Genre { get; set; }
public virtual Language Language { get; set; }
public virtual ICollection<TimeTable> TimeTables { get; set; }
}
And:
public partial class TimeTable
{
public TimeTable()
{
Reservations = new HashSet<Reservation>();
}
public int TimeTableId { get; set; }
public int MovieId { get; set; }
public int RoomId { get; set; }
public int SeatsAvaible { get; set; }
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
public virtual Movie Movie { get; set; }
public virtual ICollection<Reservation> Reservations { get; set; }
public virtual Room Room { get; set; }
}
I want to show all the records from Movie which have one or more records in TimeTable and where StartDate.date == [given datetime].
With a simple query the movies are showing multiple times. I have tried a distinct() but that changes nothing.
Anybody here who have the solution?
Current query:
var times2 =
(from s in timetablerepo.TimeTables
orderby s.StartTime.TimeOfDay
where s.StartTime.Date == datetime.Date
select s).Distinct().ToList();
Why not start with movies first and filter by timetable:
var times = timetablerepo.Movies
.Where(m => m.TimeTables.Any(t => t.StartDate.Date == <yourdate>));