milosev.com
  • Home
    • List all categories
    • Sitemap
  • Downloads
    • WebSphere
    • Hitachi902
    • Hospital
    • Kryptonite
    • OCR
    • APK
  • About me
    • Gallery
      • Italy2022
      • Côte d'Azur 2024
    • Curriculum vitae
      • Resume
      • Lebenslauf
    • Social networks
      • Facebook
      • Twitter
      • LinkedIn
      • Xing
      • GitHub
      • Google Maps
      • Sports tracker
    • Adventures planning
  1. You are here:  
  2. Home
  3. C#

Master - Detail example

Details
Written by: Stanko Milosev
Category: C#
Published: 21 November 2021
Last Updated: 30 November 2021
Hits: 2960
Here is one my example of in-memory DataTables master - detail relationship.
Create table with code like:
DataTable dtMaster = new DataTable("Master");
In this example I will add Columns without specifying type:
dtMaster.Columns.Add("Id");
Create new row:
DataRow drMaster = dtMaster.NewRow();
drMaster["Id"] = 1;
Add row to DataTable:
dtMaster.Rows.Add(drMaster);
Now details table:
DataTable dtDetail = new DataTable("Details");
dtDetail.Columns.Add("Id");
dtDetail.Columns.Add("FkDetailMasterId");

DataRow drDetail = dtDetail.NewRow();
drDetail["Id"] = 1;
drDetail["FkDetailMasterId"] = 1;
dtDetail.Rows.Add(drDetail);
Create DataSet and add relation:
DataSet ds = new DataSet();

ds.Tables.Add(dtMaster);
ds.Tables.Add(dtDetail);

DataRelation relMasterDetail = new DataRelation("MyRelation"
	, ds.Tables["Master"].Columns["Id"]
	, ds.Tables["Details"].Columns["FkDetailMasterId"]
);

ds.Relations.Add(relMasterDetail);
Example download from here.

Invoke private method with reflection

Details
Written by: Stanko Milosev
Category: C#
Published: 17 October 2021
Last Updated: 07 November 2021
Hits: 2585
I have added a class library, and introduced one private method. Invoke looks like:
MethodInfoClass mic = new MethodInfoClass();

const BindingFlags bindingFlags = BindingFlags.InvokeMethod |
								  BindingFlags.Public |
								  BindingFlags.NonPublic |
								  BindingFlags.Static |
								  BindingFlags.Instance;

System.Reflection.MethodInfo privateTestMethod = typeof(MethodInfoClass).GetMethod("PrivateTestMethod", bindingFlags);

if (privateTestMethod is not null)
{
	string returnFromPrivateTestMethod = (string)privateTestMethod.Invoke(mic, null);
	Console.WriteLine($"privateTestMethod: {returnFromPrivateTestMethod}");
}
Example project download from here.

Example of IEnumerable casting

Details
Written by: Stanko Milosev
Category: C#
Published: 29 August 2021
Last Updated: 29 August 2021
Hits: 2397
As title said:
using System;
using System.Collections;
using System.Linq;

namespace IEnumarable
{
  class Program
  {
    static void Main(string[] args)
    {
      MyIEnumerable myIEnumerable = new MyIEnumerable();

      foreach (string test in myIEnumerable.Cast<string>().ToList())
      {
        Console.WriteLine(test);
      }
    }

    class MyIEnumerable : IEnumerable
    {
      private string _names = "stanko, milosev, elizabeta, lazarevic";

      public IEnumerator GetEnumerator()
      {
        string[] aryNames = _names.Split();

        foreach (string name in aryNames)
        {
          yield return name;
        }
      }
    }
  }
}
POI:
myIEnumerable.Cast().ToList()

Filter XML nodes per value

Details
Written by: Stanko Milosev
Category: C#
Published: 29 August 2021
Last Updated: 19 September 2021
Hits: 2380
  • xml
Small example on how to filter XML per value:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace ConsoleApp15
{
    class Program
    {
        static void Main(string[] args)
        {
            string strXML = @"
                <doc>
	                <ext>Type:A</ext>
	                <ext>Type:B</ext>
                </doc>";

            XElement myXML = XElement.Parse(strXML);
            IEnumerable<XElement> xElementData = from data in myXML.Descendants("ext") where data.Value.Contains("Type:A") select data;
            foreach (XElement xElement in xElementData)
            {
                Console.WriteLine(xElement.Value);
            }

            Console.ReadKey();
        }
    }
}
POI:
IEnumerable xElementData = from data in myXML.Descendants("ext") where data.Value.Contains("Type:A") select data;
  1. Few thread examples
  2. Check if GPS position is within radius
  3. Reverse geocoding
  4. Change output path without adding target framework

Subcategories

WPF

Beginning

Code snippets

NUnit

LINQ

Windows Forms

Page 10 of 39

  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14