Click here to Skip to main content
15,881,882 members

retrieve duplicate value from linq

dartfrog asked:

Open original thread
Hi all,

I have some issue with linq statement. here is my table, named Tblactivity

activityid bigint
studentid bigint
activitydate datetime
description nvarchar(Max)
status bit

i populate Tblactivity with data. such as

activityid | studentid | activitydate | description | status
1 | 1 | 2012-08-15 00:30:00 | descA | A
2 | 3 | 2012-08-16 12:30:00 | descAA | A
3 | 2 | 2012-08-16 13:15:12 | descB | B
4 | 3 | 2012-09-02 10:20:11 | descS | A
5 | 3 | 2012-09-02 11:00:00 | descS | A
6 | 4 | 2012-09-02 14:00:00 | descAA | S

so i develop linq to get detail information of student id = 3. here is my linq code

public List<Tblactivity> getStudents(long studentid, string status)
   {
	studentdatacontext db=new studentdatacontext;
            try
            {
                List<Tblactivity> BG = (from o in db.Tblactivities
                              orderby o.activitydate descending where o.studentid == studentid &&  
                              o.activitydate.Value.Month <= DateTime.Now.Month &&
                              o.activitydate.Value.Month >= DateTime.Now.Date.AddMonths(-1)&&
                              o.status == status select o).ToList();

                return BG;
            }
            catch (Exception e)
            {
                throw e;
            }
        }


so i got all of the records according to student id = 3.but i want only one record when the activitydate is same (the latest record).
i read some article they suggest me to use MAX(). but i don't know how to develop to suit with the above function. pls suggest me.

Thanks,
df
Tags: C#, Visual Basic, .NET, LINQ, ASP.NET, MVC3

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900