Select Multiple Columns from a Table using LINQ to SQL in C#

public IOrderedEnumerable<DsGroupQueryResult> GetGroupsByName(string[] teamList)
{
using (var session = _sessionFactory.OpenSession())
{
var directoryGroup = (from dsGroup in session.Query<DirectoryGroup>()
where teamList.Contains(dsGroup.Name)
select new DsGroupQueryResult
{
TeamName = dsGroup.Name,
TeamDescription = dsGroup.Description
}).Distinct().ToList().OrderBy(x=>x.TeamDescription);
return directoryGroup;
}
}
public class DsGroupQueryResult
{
public string TeamName { get; set; }
public string TeamDescription { get; set; }
}

Create a new class for query results and in the select part of the query filled that object.

Advertisements

Author: Harshani Nawarathna

Sri Lankan woman in Computing

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s