2

I'm trying to select the max values of 3 columns from a table. All the fields being returned from the query are Strings.

What I have sofar

var step1 =  from result in t_hsbc_staging_trts_derived_tbl_ac_balance.AsQueryable()
             where result.branch_no == brnchnu
                && result.deal_id == dealid
                && result.group_mbr == grpmem
                && result.ac_type != "RMC"
             select result ;

var branch = from result in step1
             select new {ccbranch = result.cc_branch.Max()};
var sect = from result in step1
           select new { ccsect = result.cc_sect.Max()};
var dept = from result in step1
             select new { ccdept = result.cc_dept.Max()};

foreach (var result in branch)
{
  string cc_branch = result.ccbranch.ToString();
}

The error I'm getting at the foreach statement is:

Sequence operators not supported for type 'System.String'.

There must be an easier way to just get the max values from this table?

1 Answer 1

2

You are calling the Max() function on result.cc_branch which is itself a string. Even if it was successful, it would return the character of the string that has the largest unicode number, i.e.

 string s = "one-two-three";
 Console.WriteLine(s.Max()); // returns 'w'

Since I assume that is not what you want, and that you want the largest branch / section / department value, you can use:

string branch = (from result in step1 select result.cc_branch).Max();
string sect = (from result in step1 select result.cc_sect).Max();
string dept = (from result in step1 select result.cc_dept).Max();
Sign up to request clarification or add additional context in comments.

3 Comments

How do you know result.cc_branch is string ? its no where mentioned.
@Saurabh: It's mentioned in the question by the OP, and also the error is very suggestive.
With method syntax it will be even better step1.Max(r => r.cc_branch)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.