Separate statement in List <string>

I try to get various string values ​​from the Ax repository, but I get a lot of identical strings (strings contain only numbers)

var ret = context.XInventTransBackOrder .Where(i => i.BatchRouteId != "") .Select(i => i.BatchRouteId) .Distinct() .ToList(); 

Where am I going wrong?

+6
source share
3 answers

X ++ does not have a separate statement. Delayed execution will attempt to execute on ToList () and fail because of this.

+1
source

You tried

 var ret = context.XInventTransBackOrder .Where(i => i.BatchRouteId != "") .Select(i => i.BatchRouteId) .ToList(); ret = ret .Distinct() .ToList(); 
+3
source

If, for example, BatchRouteId was an XElement , it might be possible to compare object references. In this case, change the code to

 var ret = context.XInventTransBackOrder .Where(i => i.BatchRouteId != null && !String.IsNullOrEmpty(i.BatchRouteId.Value)) .Select(i => i.BatchRouteId.Value) .Distinct() .ToList(); 

UPDATE # 1

Note that some types implement implicit conversions, making you think they were a different type. You can pass the string parameter to XName without casting explicitly, and string will be automatically converted to XName .


UPDATE # 2

According to nk2003dec comment, LinqToDynamicsAx context. I do not know this interface, but it probably does not implement Distinct . In this case, you can change the context form of XY-LINQ to Object-LINQ using the extension method System.Linq.Enumerable.AsEnumerable<TSource>

 var ret = context.XInventTransBackOrder .Select(i => i.BatchRouteId) .Where(id => id != "") .AsEnumerable() .Distinct() .ToList(); 

I also inverted Select and Where , as this simplifies access to BatchRouteId

+3
source

All Articles