Hi
I have data like below and i want Code,Name to be displayed only once
| CODE | NAME | DEBIT | CREDIT |
| C01 | ABC | 500 | |
| C01 | ABC | 200 | |
| C01 | ABC | 200 | |
| C01 | ABC | 200 |
Thanks
Hi
I have data like below and i want Code,Name to be displayed only once
| CODE | NAME | DEBIT | CREDIT |
| C01 | ABC | 500 | |
| C01 | ABC | 200 | |
| C01 | ABC | 200 | |
| C01 | ABC | 200 |
Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit MohantyRank #112 · 20K reputationPosted Jan 6, 2025, 12:38 AM EST
If you want sql query, try this
WITH CTE AS ( SELECT CODE, NAME, DEBIT, CREDIT, ROW_NUMBER() OVER (PARTITION BY CODE, NAME ORDER BY (SELECT NULL)) AS RowNum FROM YourTable ) SELECT CASE WHEN RowNum = 1 THEN CODE ELSE '' END AS CODE, CASE WHEN RowNum = 1 THEN NAME ELSE '' END AS NAME, DEBIT, CREDIT FROM CTE;Jaish MathewsRank #225 · 11K reputationPosted Jan 3, 2025, 10:26 PM EST
To display
CodeandNameonly once for each group of data (based onCodeandName), you can group the data byCodeandNameand suppress repeated values in their columns. This is commonly done in reporting or rendering grouped data in a grid-like format.Here’s how you can achieve this in C# using LINQ and Blazor:
Data Example
public class Transaction { public string Code { get; set; } public string Name { get; set; } public decimal? Debit { get; set; } public decimal? Credit { get; set; } }Data Preparation
var transactions = new List { new Transaction { Code = "C01", Name = "ABC", Debit = 500, Credit = null }, new Transaction { Code = "C01", Name = "ABC", Debit = null, Credit = 200 }, new Transaction { Code = "C01", Name = "ABC", Debit = 200, Credit = null }, new Transaction { Code = "C01", Name = "ABC", Debit = 200, Credit = null }, };Group and Suppress Repeats
var groupedTransactions = transactions .Select((t, index) => new { Code = index == 0 || transactions[index - 1].Code != t.Code ? t.Code : null, Name = index == 0 || transactions[index - 1].Name != t.Name ? t.Name : null, t.Debit, t.Credit }).ToList();Render in Blazor
Here’s an example of how to render the grouped data in a Blazor component:
Code Name Debit Credit @foreach (var item in groupedTransactions) { @item.Code @item.Name @item.Debit @item.Credit }Output Table
This approach groups the data and suppresses repeated values for
CodeandName, while preserving the full detail of the transactions. Let me know if you need additional clarifications!Jignesh KumarRank #91 · 22.9K reputationPosted Jan 5, 2025, 11:05 PM EST
Hello Ramco,
Please use this once,
var data = new List { new { Code = "C01", Name = "ABC", Debit = 500, Credit = (int?)null }, new { Code = "C01", Name = "ABC", Debit = (int?)null, Credit = 200 }, new { Code = "C01", Name = "ABC", Debit = 200, Credit = (int?)null }, new { Code = "C01", Name = "ABC", Debit = 200, Credit = (int?)null }, }; var processedData = data .Select((row, index) => new { Code = index == 0 || (row.Code != data[index - 1].Code) ? row.Code : string.Empty, Name = index == 0 || (row.Name != data[index - 1].Name) ? row.Name : string.Empty, row.Debit, row.Credit }) .ToList(); Console.WriteLine($"Code \tName \tDebit\tCredit"); foreach (var row in processedData) { Console.WriteLine($"{row.Code}\t{row.Name}\t{row.Debit}\t{row.Credit}"); }This will give you resulst as below,
Code Name Debit Credit
C01 ABC 500
200
200
200