-1

在 C# EF 4 上工作。想要查找列表最大信息

    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;
            }
        }

上面的语法不起作用,在选择参数上面临问题。是否可以编写表达式树来计算最大值,基于属性。

如果有任何疑问,请询问。提前致谢。

4

1 回答 1

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.

于 2013-06-20T11:44:04.743 回答