Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Confused as how to get unique value of the attribute
Message
De
11/10/2016 12:01:37
 
 
Information générale
Forum:
ASP.NET
Catégorie:
LINQ
Versions des environnements
Environment:
C# 5.0
OS:
Windows 10
Database:
MS SQL Server
Divers
Thread ID:
01641789
Message ID:
01641830
Vues:
30
>>>Viv,
>>>
>>>For the single value it worked.
>>>
>>>However, I discovered that this code didn't work correctly:
>>>
>>>
>>> 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();
>>>
>>>The list I got back contained all rows form the templates table, not just distinct values by these 3 columns.
>>>
>>>Do you see what may be wrong here?
>>>
>>>I also tried
>>>
>>>
>>>var allValues = _matrixTemplateValuesAdapter.GetAll().Select(v => new ValueAttribute
>>>            {
>>>                ValueName = v.ValueName,
>>>                AttributeId = v.AtnameId,
>>>                Hidden = v.Hidden
>>>            }).OrderBy(x=>x.AttributeId).ThenBy(y=>y.ValueName).ToList();
>>>
>>>            matrixTemplateViewModel.AllPossibleValues = allValues.Distinct().ToList();
>>>
>>>but still got 192 values in both lists. I am supposed to get only 95 in the second list after selecting only distinct values.
>>>
>>>Thanks in advance.
>>
>>Distinct uses the default equality comparer which, for an object, will be reference equality. You will need to implement the IEquatable interface on the ValueAttribute class so that Distinct will look for value equality
>
>Thanks, it sounded a bit complex, so I just used the direct SQL command instead, e.g.
>
>
>public List<ValueAttribute> GetAllPosibleValues()
>        {
>            List<ValueAttribute> result = _siriusContext.CoreContext.ExecuteStoreQuery<ValueAttribute>
>               (@";with cte as (select t.value_name as ValueName, t.atname_id as AttributeId, t.hidden as Hidden, t.tmpnam_id as TemplateId,
>t.tmpl_order as SortOrder,  
>
>row_number() over (partition by t.atname_id, t.value_name order by t.tmpnam_id,
>
>t.tmpl_order) as Rn from dbo.i_tmplat t)
>
>select ValueName, AttributeId, Hidden
>from cte 
>where Rn = 1
>ORDER BY AttributeId, TemplateId, SortOrder").ToList();
>            return result;
>        }
>
>since I found that just SELECT DISTINCT didn't return exactly what I wanted in the right order.

That looks more complicated to me (but then, I'm not a SQL person) :-}
IAC, Distinct in linq produces an unordered result - you'd have to follow it up with an OrderBy().....
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform