0

I am working on WEB API. I have created an api which gives me details. The details should be after nth interval. Below is my code.

var mainDetails = kesc.tj_xhqd.Where(m => (m.zdjh == msn) && (m.sjsj >= dt)).AsEnumerable() .Select((x, i) => new { MSN = x.zdjh, PingDateTime = x.sjsj, PingValue = x.xhqd, i = i }) .Where(x => x.i % interval == 0) .ToList(); return Request.CreateResponse(HttpStatusCode.OK, new { details = mainDetails }); 

The output is

{ "details": [ { "MSN": "002999001180", "PingDateTime": "2018-05-16T18:39:52", "PingValue": "19", "i": 0 }, { "MSN": "002999001180", "PingDateTime": "2018-05-16T18:39:52", "PingValue": "19", "i": 24 }, { "MSN": "002999001180", "PingDateTime": "2018-05-16T18:39:52", "PingValue": "19", "i": 48 }, { "MSN": "002999001180", "PingDateTime": "2018-05-16T18:39:52", "PingValue": "19", "i": 72 }, { "MSN": "002999001180", "PingDateTime": "2018-05-16T18:39:52", "PingValue": "19", "i": 96 }, . . . . . { "MSN": "002999001180", "PingDateTime": "2018-05-16T18:39:52", "PingValue": "19", "i": 144 }, ] } 

The i value is incremented but the other details are repeated again and again. In actual the ping value comes after every 2 minutes, ping value can be same but not the date time.

Update 1

Below is my snapshot of the table taken directly from DB

enter image description here

From the above picture, it is clearly seen that only ping value i.e. 19 is repeated several times but the date time is only single but still the date time 2018-05-16T18:39:52 is shown multiple times

Update 2

Here is my DB context that will implement my model

public partial class kescEntities : DbContext { public kescEntities() : base("name=kescEntities") { this.SetCommandTimeOut(10000); } public void SetCommandTimeOut(int Timeout) { var objectContext = (this as IObjectContextAdapter).ObjectContext; objectContext.CommandTimeout = Timeout; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { throw new UnintentionalCodeFirstException(); } public virtual DbSet<tj_xhqd> tj_xhqd { get; set; } } 

How can I set my query in a way that I can get all different records after the interval?

Any help would be highly appreciated.

2
  • Currently unclear what it is you are asking. Commented Jun 26, 2018 at 11:10
  • @Nkosi actually I want to get every nth record in my table. Commented Jun 26, 2018 at 11:17

1 Answer 1

2

I've tried your code. Seems to be working as intended. It would seem that your problem lies with the data itself. Have you checked your logic for storing data? If there are duplicates in your data, naturally, duplicates will be retrieved.

Edit: I think there was a misunderstanding with the storing data part question. It's not how you store in the database I was asking, its how you were persisting the data. How do you persist your data or how the persistent data was stored? Was there any fetch logic before you arrived in this:

var mainDetails = kesc.tj_xhqd <--- This guy

The part where I tested your code:

class Program { static void Main(string[] args) { Random r = new Random(); List<Chingamajig> chinga = new List<Chingamajig>(); string[] verylongnum = new string[] { "00000000002312312", "34234002342342" }; int[] somenum = new int[] { 7, 14, 12, 28 }; for(int i = 0;i<100;i++) { Chingamajig c = new Chingamajig(); c.VeryLongNumbers = verylongnum[r.Next(0, 2)]; c.DateValue = DateTime.Now.AddMinutes((double)r.Next(-100, 101)); c.SomeNumber = somenum[r.Next(0, 4)]; chinga.Add(c); } foreach (Chingamajig c in chinga.OrderBy(x => x.VeryLongNumbers).ThenBy(y => y.DateValue).ThenBy(z => z.SomeNumber)) { Console.WriteLine("{0} : {1} : {2}", c.VeryLongNumbers, c.DateValue, c.SomeNumber); } var m = "00000000002312312"; var dt = DateTime.Now.Date; var interval = 2; var ee = chinga.Where(x => (x.VeryLongNumbers == m) && (x.DateValue >= dt)).AsEnumerable() .Select((z, i) => new { MSN = z.VeryLongNumbers, PingDT = z.DateValue, PingV = z.SomeNumber, i = i }) .Where(x => x.i % interval == 0).ToList(); Console.Clear(); foreach(var n in ee) { Console.WriteLine("{0} : {1} : {2} : {3}", n.MSN, n.PingDT, n.PingV, n.i); } Console.ReadLine(); } } public class Chingamajig { public string VeryLongNumbers { get; set; } public DateTime DateValue { get; set; } public int SomeNumber { get; set; } } 

So yeah.. I did test your code with mock data because your sample code had little meat in it.

Update: Try adding this to your code

var mainDetails = kesc.tj_xhqd //This line .AsNoTracking() .Where(m => (m.zdjh == msn) && (m.sjsj >= dt)) .AsEnumerable() .Select((x, i) => new { MSN = x.zdjh, PingDateTime = x.sjsj, PingValue = x.xhqd, i = i }) .Where(x => x.i % interval == 0) .ToList(); 

Not sure but this problem seems to be stemming from tables with no pkey. Not sure about your case though but below is a link for your reference Identical Rows

Sign up to request clarification or add additional context in comments.

11 Comments

Ping value can be duplicated but not the time
And also in the table, there only one record against the time 2018-05-16 18:39:52. As it is the 1st record so only this one is showing multiple times
@MrFaisal I've edited my answer to explain further that its not about your data in the database but how that persistent data is stored/fetched.
There is no fetch logic only some where clauses.
Always glad to help! Cheers! :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.