0

i have been retrieving data from datatable using linq.i have retrieved the data successully too.the problem is ,i have to show only one record if two records have same projectname (irrespective of which record,but one record must be shown from the dupicate records).

for multiple records with same projectname, only one record from it should be shown.

I have pasted my working code.

cmd.CommandText = " Select PROJECTNAME,COMPANY,PROJECTSTATUS,STARTEDIN,COMPLETEDIN FROM CMPPROJECT WHERE STATUS ='" + a + "'";
            using (OracleDataAdapter sda = new OracleDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    dt.TableName = "CMPPROJECT";
                    sda.Fill(dt);
                     var select = (from ab in dt.AsEnumerable()
                                   select new { c1 = ab["PROJECTNAME"], c2 = ab["COMPANY"], c3 = ab["PROJECTSTATUS"], c4 = ab["STARTEDIN"], c5 = ab["COMPLETEDIN"] }).Distinct().ToList();
                    dt.Clear();
                    foreach (var item in select)
                 dt.Rows.Add(item.c1, item.c2, item.c3, item.c4, item.c4);
                 return dt;
             }

Please help me with modified working code..

1
  • If your code is working, what's the problem then? Commented Nov 11, 2015 at 9:10

1 Answer 1

1

You can use GroupBy:

var projectGroups = dt.AsEnumerable()
    .Select(row => new
    {
        project = row.Field<string>("PROJECTNAME"),
        company = row.Field<string>("COMPANY"),
        status = row.Field<string>("PROJECTSTATUS"),
        startedin = row.Field<DateTime>("STARTEDIN"),    // presumes a DateTime-column
        completedin = row.Field<DateTime>("COMPLETEDIN") // presumes a DateTime-column
    })
    .GroupBy(x => x.project)
    .Select(g => g.First())
    .Distinct()
    .ToList();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.