-1

Work on C# EF 4. Want to find list max information

    selector= entityBillax.BillTaxID

  public static List<T> GetMaxRowIDForChild2<T>(T fromList, Expression<Func<T,T>> selector)
        {        
            selector = fromList.Count + 1;

            if (fromList.Count > 0)
            {
                selector = fromList.Max(x => x.BillTaxID) + 1;
            }
            else
            {
                selector = 1;
            }
        }

Above syntax not working ,Face problem on select parameter.IS it possible to write expression tree for calculate the max ,base on property.

If have any query please ask.Thank in advanced.

shamim
  • 6,640
  • 20
  • 85
  • 151
  • the `selector` here always returns `bool`; how do you "max" a bool? – Marc Gravell Jun 20 '13 at 09:54
  • Marc Gravell ,thanks for reply.Hope you understand my requirement,if need any correction please do that just help me to make it as workable syntax. – shamim Jun 20 '13 at 09:57
  • 1
    no, I don't understand the requirement - hence my comment; again, seriously: what is the usage here? it is very unclear. Indeed, `selector` (lower-case `s`) isn't used at all, and `Selector` (upper-case `S`) is used in a way that really really is not obvious. Please can you explain what you are trying to do, because the code doesn't make it obvious – Marc Gravell Jun 20 '13 at 09:58
  • I'm really confused by this code. `fromList` should be a list, but its type says it's a single item. The method should return a single item, but its return type is a list. The method should return its result, but all you do is to assign some values that don't make much sense to `selector`. – svick Jun 20 '13 at 11:33

1 Answers1

1

I have no idea if this is what you're asking, but if you have an IQueryable representing some database table and an Expression that selects an integer property from that table and you want to get the max value of that property + 1 (or 1 if the table is empty), then you can do something like this:

public static int GetMaxRowId<T>(
    IQueryable<T> source, Expression<Func<T, int>> selector)
{
    if (source.Any())
        return source.Max(selector) + 1;

    return 1;
}

Though this code has some issues, but it should work as a base for your real code.

svick
  • 236,525
  • 50
  • 385
  • 514