Problem 1 - Solving real world problem using datatable (English Version) - Updating datatable

preview_player
Показать описание
In this video I will try to explain a particular problem which is given to me on my job. I have try my best to make it as simple as possible so that we can focus more on problem rather than on data.

I will be using MVC project and code will be in c#

Few things we learn after watching the video.

1. How find unique records in array of string from datatable.
2. How filter datatable using linq on the basis on array of string.
3. How to create datatable from scratch.
4. How to update datatable using for loop and datarow.
filter datatable with string array c#

#myzingonline #myzingonlineenglish #realworldproblem #programmingexperince
------------------------------------------------
Source code: please use url decode utility for decoding source code as youtube is not allowing angular brackets.
--------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
namespace RealWorldProblemSolution.Models.Problem1
{
public class Problem1
{
public void GetProccessedData() {
string[] uniqueTerms = null;
string excelColumnName = "SearchTermA";
DataTable dtSearchTerm = GetSearchTermData();
// Get the unique terms for table
uniqueTerms = (from r in dtSearchTerm.AsEnumerable()
select r.Field%3Cstring%3E(excelColumnName))
.Where(r =%3E !string.IsNullOrEmpty(r)).Distinct().ToArray();
var dtSearchTermA = GetSearchResult(uniqueTerms);
//excelColumnName = "SearchTermB";
uniqueTerms = (from r in dtSearchTerm.AsEnumerable()
select r.Field%3Cstring%3E("SearchTermB"))
.Where(r =%3E !string.IsNullOrEmpty(r)).Distinct().ToArray();
var dtSearchTermB = GetSearchResult(uniqueTerms);
//DataTable pResult = GetSearchResult(uniqueTerms);
//return pResult;
foreach (DataRow row in dtSearchTerm.Rows)
{
foreach (DataRow rowA in dtSearchTermA.Rows)
{
if (row["SearchTermA"] == rowA["SearchTerm"]) {
row["UrlForA"] = rowA["Url"];
}
}
foreach (DataRow rowB in dtSearchTermB.Rows)
{
if (row["SearchTermB"] == rowB["SearchTerm"])
{
row["UrlForB"] = rowB["Url"];
}
}
}
}
//Can hold lakhs of record
private DataTable GetSearchTermData() {
DataTable dtSearchTermData = new DataTable();
dtSearchTermData.Columns.Add("SearchTermA", typeof(string));
dtSearchTermData.Columns.Add("SearchTermB", typeof(string));
dtSearchTermData.Columns.Add("UrlForA", typeof(string));
dtSearchTermData.Columns.Add("UrlForB", typeof(string));
dtSearchTermData.Rows.Add("A", "A1");
dtSearchTermData.Rows.Add("B", "B1");
dtSearchTermData.Rows.Add("C", "C1");
dtSearchTermData.Rows.Add("A", "D1");
dtSearchTermData.Rows.Add("E", "A1");
return dtSearchTermData;
}

private DataTable GetDetailsData()
{
DataTable dtResultData = new DataTable();
dtResultData.Columns.Add("SearchTerm", typeof(string));
dtResultData.Columns.Add("Url", typeof(string));
dtResultData.Rows.Add("A", "www.A.com");
dtResultData.Rows.Add("B", "www.B.com");
dtResultData.Rows.Add("C", "www.C.com");

return dtResultData;
}

private DataTable GetSearchResult(string[] searchTerm,string columnName= "SearchTerm")
{
DataTable dt = GetDetailsData();
string searchTermComma = "(" + string.Join(",", searchTerm) + ")";
var filteredDt = dt.AsEnumerable()
.Where(r =%3E searchTerm.Contains(r.Field%3Cstring%3E(columnName))).CopyToDataTable();
return filteredDt;
}
}
}

Social Links: [FOLLOW]

Social network section
YouTube User ID: ivd6M0sL-YaWMgwAvDtfOA
YouTube Channel ID: UCivd6M0sL-YaWMgwAvDtfOA
Рекомендации по теме