Skip to content

I have a scenario in C# using linq... pls guide me

Prabu Baskaran

Asked Aug 18, 2014, 10:13 PM EDT1 answer
Share
LinkedInX
creating a table with primary key as ID.
using linq i am checking the date.
 
eg:  1st .txt file has this values.
ID,name,Valid_END_DATE
1,raja,15-07-2013
2,ram,13-08-2014
 
2nd .txt file has this values
ID,name,Valid_END_DATE
1,raja,15-07-2014       //condition checks   
2,ram,13-08-2014 
 
 
//---->here the condition checks if it's true it should create a new ID(eg)3 and update the value in the database, else(ie) false it should update with Same ID. 
 
 
Exactly what i need in database
1,raja,15-07-2013
2,ram,13-08-2014
3,raja,15-07-2014 
 
here is my coding. using c# 
 
C#
CultureInfo enGB = new CultureInfo("en-GB");
                    DateTime CheckDate = DateTime.Parse(csv[12], enGB);
                    string primary = csv[0].ToString();          //Primary key from this Table.
                    if (!string.IsNullOrEmpty(primary))
                    {
                        using (EntityModel entities = new EntityModel())
                        {
                            SSS objRMPRC = (from o in entities.SSSs
                                            where o.ID == primary && o.VALID_END_DATE <= CheckDate
                                            select o).FirstOrDefault();

                            if (objRMPRC == null)
                            {
                                objRMPRC = SSS.CreateSSS(primary);
                                entities.AddToSSSs(objRMPRC);
                            }

                            for (int i = 0; i < fieldCount; i++)
                            {

                                string ss = csv[i].ToString(); // to check the values.

                                csv.SkipEmptyLines = true;// Skips Empty line

                                if (i == 12)        //Datetime Format Column values
{ //en-GB: dd/MM/yyyy (e.g. 14/03/2012)
Java
                                    //CultureInfo enGB = new CultureInfo("en-GB");         //en-US: M/d/yyyy (e.g. 3/14/2012)
                                    DateTime MyDateTime = DateTime.Parse(csv[i], enGB);  //Language is set to UK english (en-GB) in windows control panel and web.config.
                                    objRMPRC.GetType().GetProperty(headers[i]).SetValue(objRMPRC, MyDateTime, null);
                                }
                                else
                                {
                                    objRMPRC.GetType().GetProperty(headers[i]).SetValue(objRMPRC, csv[i], null);
                                }
                            }
entities.SaveChanges(); //Save the Lines into Database.
}//using
}//if
 

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    what i got from your requirement is you want to insert new record if there is mismatch between 1.txt & 2.txt? And identical records should remain same? Please confirm
    0