0

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();
  • Did you try `where c.GroupName == "company"` ? – Chronicle Jan 27 '21 at 15:54
  • @Chronicle Sorry my question is not that clear. I want to show all the group names like 'Group', and other stuff in group names but in GroupName company I only want to show only forbes just filtering out other companies – Eugene Cabangon Jan 27 '21 at 15:57

2 Answers2

2

You can add a where statement in the query.

where c.GroupName == "company" && c.Display == "Forbes"
Tah
  • 1,526
  • 14
  • 22
  • I'm not a fan of query syntax, I prefer method syntax, but I always thought that when using query syntax you cannot use equality operator. Shouldn't you use the word `equals` instead, or am I completely wrong? – Harald Coppoolse Feb 17 '21 at 07:00
  • The C# reflector for `==` use the `Equals` method https://stackoverflow.com/questions/5796169/vs-string-equals-c-sharp-net so the usage is identical. This may be more preferable in C# because of `NullReferenceExceptions` that could occur if the first string is null. – Tah Feb 26 '21 at 00:09
1

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
    {
       ...
    });
Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116