new to linq and wcf contracts

I am hving trouble trying to figure out how to return a list type when I set up the select to return specific columns as opposed to the all the columns. Not sure if I should be returning a list or another type. this is what I have so far not sure of the exact syntax.
[OperationContract]
        public List<> GetCerts()
        {
            SALARYDCDataContext db = new SALARYDCDataContext();
            // var selected_rows = from row in db.TBLCERTs select row;
            //return selected_rows.ToList();
            var GC = from c in db.TBLCERTs
                     select new
                     { c.CERTID,
                       Certification = c.CERTCODE + " - " + c.CERTDESC
                      };
                    return GC.ToList();
        }

Open in new window

NoSleepForTheWearyAsked:
Who is Participating?
 
Fernando SotoConnect With a Mentor RetiredCommented:
Hi NoSleepForTheWeary;

What I would do is to create a class like the GCData in the code snippet below and then when you create the new item in the select create it as a new GCData also in the code snippet. The class must be accessible from where ever you use it.

Fernando
[OperationContract]
public List<GCData> GetCerts()
{
    SALARYDCDataContext db = new SALARYDCDataContext();
    // var selected_rows = from row in db.TBLCERTs select row;
    //return selected_rows.ToList();
    var GC = from c in db.TBLCERTs
             select new GCData
             { 
                 CERTID = c.CERTID,
                 Certification = c.CERTCODE + " - " + c.CERTDESC
             };
            return GC.ToList();
}
 
 
 
public class GCData
{
    String CERTID { get; set; }
    String Certification { get; set; }
}

Open in new window

0
 
NoSleepForTheWearyAuthor Commented:
I tried the code in the service.svc.cs file and it gave me 2 errors.
'SALARY.Web.GCData.CERTID' is inaccessible due to its protection level
'SALARY.Web.GCData.Certification' is inaccessible due to its protection level
not sure what to make of these.
0
 
Fernando SotoRetiredCommented:
Hi NoSleepForTheWeary;

The statement in my last post, "The class must be accessible from where ever you use it.", In your scenario it must be defined with in the web service project/class and it also needs to be defined in the client project/class.

Fernando
0
 
NoSleepForTheWearyAuthor Commented:
ok figured it out needed to make the int and string a public member of the class. thanks for the help
public class GCData
    {
        public int CERTID { get; set; }
        public String Certification { get; set; }
    } 

Open in new window

0
 
Fernando SotoRetiredCommented:
Not a problem, glad I was able to help.  ;=)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.