Eugene Cabangon
Eugene Cabangon

Reputation: 29

LINQ: select specific value in a datatable column

In table I have 4 Columns GroupName, Display, Value and ID How can I just show a specific data in display. I only want to show some of the groupNames Data for example I only want to show Groupname = company and display = Forbes

Here's my linq

                sample = (from c in smsDashboardDBContext.CodeDefinitions
                                  orderby c.Display ascending
                                                                    
                                  select new CodeDefinitionDTO

                                  {
                                      GroupName = c.GroupName,
                                      Display = c.Display,
                                      Value = c.Value,
                                      Id = c.Id

                                  }).ToList();

Upvotes: 0

Views: 198

Answers (2)

Harald Coppoolse
Harald Coppoolse

Reputation: 30454

I only want to show some of the groupNames Data for example I only want to show Groupname = company and display = Forbes

Before the ToList, use a Where to keep only those items that you want to show:

var company = ...
var forbes = ...
var result = smsDashboardDBContext.CodeDefinitions
    .OrderBy(codeDefinition => codeDefintion.Display)
    .Select(codeDefinition => new CodeDefinitionDTO
    {
        Id = codeDefinition.Id,
        GroupName = codeDefinition.GroupName,
        Display = codeDefinition.Display,
        Value = codeDefinition.Value,
    })
    .Where(codeDefinition => codeDefition.GroupName == company
                          && codeDefintion.Display == forbes);

In words:

  • Order all codeDefinitions that are in the table of CodeDefintions by ascending value of property codeDefintion.Display.
  • From every codeDefinition in this ordered sequence make one new CodeDefinitionDTO with the following properties filled: Id, GroupName, Display, Value
  • Frome every codeDefintion in this sequence of CodeDefinitionDTOs, keep only those codeDefinitions that have a value for property GroupName that equals company and a value for property Display that equals forbes.

There is room for improvement!

Suppose your table has one million elements, and after the Where, only five elements are left. Then you will have sorted almost one million elements for nothing. Consider to first do the Where, then the Order and finally a Select.

In LINQ, try to do aWhere as soon as possible: all following statements will have to work on less items

In LINQ, try to do a Select as late as possible, preferrably just before the ToList / FirstOrDefault / ... This way the Select has to be done for as few elements as possible

So first the Where, then the OrderBy, then the Select, and finally the ToList / FirstOrDefault, etc:

var result = smsDashboardDBContext.CodeDefinitions
    .Where(codeDefinition => ...);
    .OrderBy(codeDefinition => codeDefintion.Display)
    .Select(codeDefinition => new CodeDefinitionDTO
    {
       ...
    });

Upvotes: 1

Tah
Tah

Reputation: 1536

You can add a where statement in the query.

where c.GroupName == "company" && c.Display == "Forbes"

Upvotes: 2

Related Questions