Asha
Asha

Reputation: 11

Convert the sql query to linq

I have the following block of code which works fine;

var toDoSessionsInDB = from ZSESSION todo in todoZession.ZSESSIONs
                       select todo ;

ToDoZessions = new ObservableCollection<ZSESSION>(toDoSessionsInDB);

I wanted to modify the 'select' part, But the following code is throwing error

var toDoSessionsInDB = from ZSESSION todo in todoZession.ZSESSIONs
                       select  ContactName
                                  .Substring(0,c.ContactName.IndexOf(' ')),Age,ContactNumber;
ToDoZessions = new ObservableCollection<ZSESSION>(toDoSessionsInDB);

Following error shows before compilation ... .... The best overloaded method match for System.Collections.ObjectModel.ObservableCollection.ObservableCollection(System.Collections.Generic.List) has some invalid arguments

Any help ?

Upvotes: 0

Views: 118

Answers (1)

Robaticus
Robaticus

Reputation: 23157

If you're trying to select more than one thing, you're going to need to select new in your LINQ. It would look something like this:

var toDoSessionsInDB = from todo in todoZession.ZSESSIONs
                       select new ZSESSION
                       {
                         Name = todo.ContactName.Substring(0,c.ContactName.IndexOf(' ')),
                         Age = todo.Age,
                         ContactNumber = todo.ContactNumber
                       };

Where the items on the left side of the equals sign are the properties in your ZSESSION class. You didn't provide that class, so I had to guess at their names.

Upvotes: 1

Related Questions