Export to csv - Linq query?

In order to control the sort order you must define a class with attributes like.

Up vote 3 down vote favorite share g+ share fb share tw.

Thanks! Var usr = from usr in db. User select new { usr.

UserName, usr. Dept, usr. Name) MainListView.

DataSource = usr; MainListView.DataBind(); CsvFileDescription outputFileDescription = new CsvFileDescription { SeparatorChar = ',', FirstLineHasColumnNames = true, FileCultureName = "en-US" }; CsvContext cc = new CsvContext(); string finalPath = mypath + "usr_" + DateTime.Now. ToString ( "yyyyMMddhhmmssfff" ) + ". Csv"; cc.

Write( usr, finalPath, outputFileDescription ); c# linq linq-to-entities link|improve this question edited Feb 16 at 23:27 asked Feb 16 at 0:50FAA385 67% accept rate.

In order to control the sort order you must define a class with attributes like public class User { CsvColumn(FieldIndex = 1) public string UserName { get; set; } CsvColumn(FieldIndex = 2) public string Dept { get; set; } CsvColumn(FieldIndex = 3) public string Name { get; set; } } Then you change your linq query to use this class, eg string mypath = @". \"; var usr = (from you in db. User select new User { UserName = u.

UserName, Dept = u. Dept , Name = u. Name } ); CsvFileDescription outputFileDescription = new CsvFileDescription { SeparatorChar = ',', FirstLineHasColumnNames = true, FileCultureName = "en-US" }; CsvContext cc = new CsvContext(); string finalPath = mypath + "usr_" + DateTime.Now.

ToString("yyyyMMddhhmmssfff" ) + ". Csv"; cc. Write( usr, finalPath, outputFileDescription ).

I tried that, but I have date and time field that won't accept it. I have it in the class defined as dateTime and in the sql is date and time too. Any ideas.

Best regards – FAA Feb 20 at 22:15 If the sql column allows nulls then you might need to use DateTime? Instead of DateTime. If that doesn't help, then what error do you get?

– sgmoore Feb 21 at 9:13 it is datetime on both. Also the error is: DateTaken = u. DateTaken structure (see msdn.microsoft.com/en-us/library/b3h38hb... for more details).

DateTime? Is just a shorthand for Nullable. The same issue can arise with most other Sql types such as bit, money etc. – sgmoore Feb 21 at 19:51.

Since you're already familiar with LINQ, I've used Matt Perdeck's LINQ2CSV ( codeproject.com/Articles/25133/LINQ-to-C... ) before and it's made my life a lot easier.

I am looking to have a button where the user press on it, then it prompt for save file to their computer. Also is there an easier way to do this. Thanks – FAA Feb 16 at 5:51 Any other comments!

– FAA Feb 16 at 18:32 I have used that Linq2csv and used it this way (as updated on the first post). But I get the output to csv but column order is random. Any ideas of how to fix this?

– FAA Feb 16 at 23:13 Do you mean the order of the actual columns "UserName", "Dept", "Name" come out in random orders (sometimes "Name" then "UserName" then "Dept") or that the actual data ROWS are random? (not alphabetic? ).

You probably just need to add a .OrderBy() to your linq expression. – rally25rs Feb 16 at 23:36 there is an orderby already and that works fine. But the problem is: the columns not the rows.

They should be: username, dept, name. But they are not. Any ideas!

Thanks – FAA Feb 167 at 0:44.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions