none
Getting distinct integer value RRS feed

  • Question

  • Hi everybody,

    I have this part of the LINQ query working

     var query = matrixTemplateViewModel.TemplateValues.Where(x => x.AtnameId != matrixTemplate.PrimAxis);

    I need to get distinct value of that x.AtnameId back. I attempted the following which doesn't compile

    ColumnAttributeId = query.Select(x => new { x.AtnameId }).Distinct().FirstOrDefault();

    and gives this error

    'Cannot implicitly convert type < anonymous type: int AtnameId to int > '

    How can I get the unique scalar value from the above query?

    Thanks in advance.


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles

    Monday, October 10, 2016 5:13 PM

Answers

  • Got it working with a very simple change:

    ColumnAttributeId = query.Select(x => x.AtnameId ).Distinct().FirstOrDefault();


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles

    • Marked as answer by Naomi N Monday, October 10, 2016 6:17 PM
    Monday, October 10, 2016 6:17 PM

All replies

  • Got it working with a very simple change:

    ColumnAttributeId = query.Select(x => x.AtnameId ).Distinct().FirstOrDefault();


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles

    • Marked as answer by Naomi N Monday, October 10, 2016 6:17 PM
    Monday, October 10, 2016 6:17 PM
  • I have a similar query where I need to get distinct 3 columns from my table. This is the code I am using

    matrixTemplateViewModel.AllPossibleValues = _matrixTemplateValuesAdapter.GetAll().Select(v => new ValueAttribute
                {
                    ValueName = v.ValueName,
                    AttributeId = v.AtnameId,
                    Hidden = v.Hidden
                }).Distinct().OrderBy(x=>x.AttributeId).ThenBy(y=>y.ValueName).ToList();

    However, it doesn't return distinct values but returns the whole set. What should I change in order to get distinct values by these 3 columns?


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles

    Monday, October 10, 2016 8:04 PM