I need to select 2 columns of a table which are long data type and find the largest number among both columns from the result. What I tried but not successful is the following (this has errors because its incomplete code):
public long GetMaxAddressByStation(StationType station)
{
long maxAddress = 0;
var addressList = (from tbl1 in Table1
join tbl2 in Table2 on tbl1.Guid equals tbl2.Guid
where tbl2.Station == station
select new
{
tbl1.DataAddress,
tbl1.CommandAddress
}).Max(<condition>);
return maxAddress;
}
The problem you hare facing by trying to use the
Max()method fromIEnumerable(orIQueryable, which inherits the method from the previous), is that it is supposed to be used to get the maximum element from a list, not from several values in an element of a list.That means you will compare your sets of addresses one to the other, instead of comparing the addresses of each of your set, to keep the greatest.
To solve this issue, I would create a method somewhere (probably with private visibility, directly in the class unless you see any use in keeping it somewhere else) that would take the enumerable of address sets and return an enumerable of long:
And taking back your code, you'd call it that way: