Max() is an aggregate function used to find the largest value in a set (collection). Your example code is a single item not a collection. Please make a better effort to read the documentation and learn basic C#. Frankly, you code makes no logical sense...
Aggregate Functions (Transact-SQL)
Enumerable.Max Method (Example code is located at the bottom of the documentation)
LINQ Max() example.
class Program
{
static void Main(string[] args)
{
List<Developer> data = PopulateMockData();
int maxId = data.Max(d => d.Id);
Console.WriteLine($"maxId = {maxId}");
int nextId = maxId + 1;
Console.WriteLine($"nextId = {nextId}");
}
public static List<Developer> PopulateMockData()
{
List<Developer> data = new List<Developer>();
for (int i = 0; i < 10; i++)
{
data.Add(new Developer() { Id = i, EnvT = $"Ent_{i}" });
}
return data;
}
}
public partial class Developer
{
public int Id { get; set; }
public string EnvT { get; set; }
}
Results
maxId = 9
nextId = 10
If you understand LINQ then you must understand that your design makes no logical sense. LINQ operates over a collection not a single item.
I explained why your design does not work, provided example code, and links to resources. If you do not understand the code or the explanation or the links then there is not much the community can do to your help. You need set aside time to learn the LINQ and C# fundamentals.
The compiler error in your previous threads was due to you not understanding how to concatenate a string or solve a very basic programming problem.
There is no error with a concatenate the value.
I understand that a max can be write in several ways :
1 - math.max(a,b) with a, b int
2 - datasource.max( "with linq action") that will be operate for a data collection (list e.g).
Anyway, in my case I want to implement the max function for the column Id in class model. So is it possible or not?
If yes, How can I do it?
Thanks in advance
What you are trying to do is not possible or logical.
Your entity design can not query data, set data, or fetch data from a Developer table. The design is useless as an entity. This logic should exist in a business layer not data access layer. I would write a standard .NET Core service and use dependency injection.
See any getting started tutorial.
Ok. If I do understand is not possible to put the datasource.max() in class model. It's seem so logic!
instead of doing a stored procedure to calculate an increment of the form "001/22" I wanted to do it directly in the model class
Placing the data source in an entity would cause an infinite loop. As Spock would say, the design is illogical.
If you are referring to an Entity Framework model, an entity, then this is also illogical. Entities model a table but you are designing logic that acts on data. This type of logic is written in a business logic layer not entity framework which is the data access layer. For the second time, design a standard .NET service to encapsulate this logic. Use dependency injection to invoke the logic. Keep in mind, that this logic runs in the browser.
great you have a sense of fun :-)
I guess I'm not asking you how we can do it! ;-)
I've provided sample T-SQL in your other post and sample LINQ in this post. I'm not sure what else I can do for you.
I remember well that you give me T-SQL to make the stored procedure. So I was not able to implement the strored procedure in blazor.
Plus, this example is not applicable in my case because I don't have the input [Year] in my table.
The code I presented in your other thread was intended to illustrate how to take a requirement, come up with a design, and run the code through tests to make sure the code works as expected. You were supposed to use the code as a guide. Instead you copied and pasted the code into a procedure and complain your table does not have a Year column. Which by the way is absurd since your requirement specifically uses the current year to create formatted string Id. The current year is also needed to determine if the Id should restart at 1 when the year changes.
Year is just a name. You could call it Spock for all intent and purposes. In my opinion Year makes more sense whatever is clever.
With that being said, I still do not understand why you are unable to write basic logic or solve basic problems. Clearly, the community cannot write your code because you do not understand C#, LINQ, SQL, types, or collections. It seems we are at a standstill. You'll need to educate yourself to move forward.
Sign in to comment