Posts

Showing posts with the label linq

C# Method to Compare DateTime Fields Specified During Execution?

C# Method to Compare DateTime Fields Specified During Execution? My project has many objects with date fields, and I often need to select everything where one such field is within a date range. For example: public class Contract { public DateTime SignDate { get; set; } public DateTime ReleaseDate { get; set; } } public class PersonalCheck { public DateTime SignDate { get; set; } public DateTime ProcessDate { get; set; } public DateTime VoidDate { get; set; } } If I only cared about SignDate, it would be easy. I would declare an Interface... public interface IObjectWithSignDate { DateTime SignDate { get; set; } } ...change my other objects to inherit from it, then create a method like this: public static IQueryable<T> SignedWithin<T>(this IQueryable<T> items, DateTime start, DateTime end) where T : IObjectWithSignDate { return items.Where(q => q.SignDate >= start && q.SignDate <= end); } How can I avoid rewr...

IQueryable to return a single record

IQueryable<T> to return a single record I am using EF Core and am attempting to create a query provider which accepts any type and a single value. I then want to run a query to return the FirstOrDefault of the passed in items which have the selected property set to the passed in value. something similar to this: public class TagPicker<T> { public IQueryable<T> Pick(IQueryable<T> source, string collumn, string filter) { T result = source.FirstOrDefault(r => r.collumn == value); if (result is null) { return new T { collumn = filter }; } else { return result; } } } I have several types which will need this type of query performed. I am trying to avoid having to create duplicates of this type of query for each type. Any Ideas? What problem are you trying to solve with this code? This looks like an attempt to replicate Where() or FirstOrDefault(predicat...

Unable to concatenate two IEnumerable lists [duplicate]

Unable to concatenate two IEnumerable lists [duplicate] This question already has an answer here: I am creating a WEB API . I have two IEnumerable lists. And at the end I want to concatenate them. WEB API IEnumerable IEnumerable result; IEnumerable prodDetails = new List<tj_xhqd>(); IEnumerable mainDetails= new List<tj_xhqd>(); int prodInterval, prodCount = 0; int mainInterval, mainCount = 0; prodCount = giveProdCount(msn, dt); if(prodCount==0) { prodDetails = ""; } else if (prodCount<=500) { prodDetails = mdcEntitites.tj_xhqd .Where( m => (m.zdjh == msn) && (m.sjsj >= dt) ) .Select( x => new { MSN = x.zdjh, PingDateTime = x.sjsj, PingValue = x.xhqd } ) .ToList(); } else { prodInterval = prodCount / 500; prodDetails = mdcEntitites.tj_xhqd .AsNoTracking() .Where( m => (m.zdjh == msn) && (m.sjsj >= dt) ) .AsEnumerable() .Select( (x, i) => new { MSN = x.zdjh...