none
LinQ - find the first free position RRS feed

  • Question

  • Hello,
    I have to read a file into my own structure. I have to edit the next free Position.
    <PANEL no="3" state="0" finished="0">
        <INDEX no="2" state="0" ref1="0003"  />
    	
    	modify it to 
    	
       <INDEX no="2" state="0" ref1=""new ref 1 for test"  />
    Problems:
      How do I make sure that I ask for the first free position?
      Is it sufficient with sorting / order by?
     
     
    var qry = ListIndexPanels.Where(x => x.Finished == 0).OrderBy(o => o.No).FirstOrDefault();
    if (qry != null)
    {
    	var qry2 = qry.ListIndexProperties.Where(x => x.No == 2).FirstOrDefault();
    	if (qry2 != null)
    	{
    		qry2.ref1 = "new ref 1 for test";
    
    	}
    }
    <WORKS_ORDER id="0601">
      <QUANTITY>3</QUANTITY>
      <PRODUCT >
        <PANELS>
          <PANEL no="1" state="0" finished="1">
            <INDEX no="1" state="0" ref1="0001"  />
            <INDEX no="2" state="0" ref1="0001"  />
          </PANEL>
          <PANEL no="2" state="0" finished="1">
            <INDEX no="1" state="0" ref1="0002"  />
            <INDEX no="2" state="0" ref1="0002"  />
            <INDEX no="3" state="0" ref1="0002"  />
            <INDEX no="4" state="0" ref1="0002"  />
            <INDEX no="5" state="0" ref1="0002"  />
            <INDEX no="6" state="0" ref1="0002"  />
            <INDEX no="7" state="0" ref1="0002"  />
          </PANEL>
          <PANEL no="3" state="0" finished="0">
            <INDEX no="1" state="0" ref1="0003"  />
            <INDEX no="2" state="0" ref1="0003"  />
            <INDEX no="3" state="0" ref1="0003"  />
          </PANEL>
    	    <PANEL no="4" state="0" finished="0">
            <INDEX no="1" state="0" ref1="0004"  />
            <INDEX no="2" state="0" ref1="0004"  />
            <INDEX no="3" state="0" ref1="0004"  />
          </PANEL>
        </PANELS>
      </PRODUCT>
    </WORKS_ORDER>
    
    		




    Saturday, June 30, 2018 10:02 AM

Answers

  • Hello Karen,

    yes looks not bad, if I can read my XML directly in a dataset.

       Inside dataset it is not depended from element or attribute? 

        Column name is the same as element, now it can be also a attribute, right?

    Can you show me, how I  can do it, if I want the first free unfinished number?

    <PANEL no="3" state="0" finished="0">

    With best regards Markus


    Here you go

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.IO;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                Shown += Form1_Shown;
            }
    
            private void Form1_Shown(object sender, EventArgs e)
            {
                var ds = new DataSet();
                var fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "File1.xml");
                ds.ReadXml(fileName);
    
                var panelData = ds.Tables["PANEL"]
                    .AsEnumerable()
                    .Select(row => new PanelData()
                    {
                        Id = row.Field<int>("PANEL_id") + 1,
                        Finished = row.Field<string>("Finished")
                    } );
    
    
                var result = ds.Tables["INDEX"]
                    .AsEnumerable()
                    .Select(row => new PanelItem
                    {
                        Id = row.Field<int>("PANEL_id") + 1,
                        No = Convert.ToInt32(row.Field<string>("no")),
                        Ref = row.Field<string>("ref1")
                    })
                    .ToList();
    
                List<List<PanelItem>> groupedList = result
                    .GroupBy(u => u.Id)
                    .Select(grp => grp.ToList())
                    .ToList();
    
                int id;
                string finished;
                foreach (var item in groupedList)
                {
                    // ReSharper disable once PossibleNullReferenceException
                    id = item.FirstOrDefault().Id;
                    finished = panelData.FirstOrDefault(curr => curr.Id == id).Finished;
    
                    // ReSharper disable once PossibleNullReferenceException
                    Console.WriteLine($"Id: {id} - " + 
                                      $"last used No: {item.Select(p => p.No).Max() +1} " + 
                                      $"Finished: {finished}");
                }
            }
        }
    
        /// <summary>
        /// Should reside in a separate file
        /// </summary>
        public class PanelItem 
        {
            public int Id { get; set; }
            public int No { get; set; }
            public string Ref { get; set; }
        }
    
        public class PanelData
        {
            public int Id { get; set; }
            public string Finished { get; set; }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, July 1, 2018 12:09 PM
    Moderator

All replies

  • Sorry, it's absolutly unclear what you mean by "next free position".
    Saturday, June 30, 2018 10:30 AM
  • Hello,

    Perhaps this is what you are looking for, get last No. In this case you can add 1 to the max value. I took your XML, read it in, created a typed list then grouped on Id.

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.IO;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                Shown += Form1_Shown;
            }
    
            private void Form1_Shown(object sender, EventArgs e)
            {
                var ds = new DataSet();
                var fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "File1.xml");
                ds.ReadXml(fileName);
    
                var result = ds.Tables["INDEX"]
                    .AsEnumerable()
                    .Select(row => new PanelItem
                    {
                        Id = row.Field<int>("PANEL_id") + 1,
                        No = Convert.ToInt32(row.Field<string>("no")),
                        Ref = row.Field<string>("ref1")
                    })
                    .ToList();
    
                List<List<PanelItem>> groupedList = result
                    .GroupBy(u => u.Id)
                    .Select(grp => grp.ToList())
                    .ToList();
    
                foreach (var item in groupedList)
                {
                    Console.WriteLine($"Id: {item.FirstOrDefault().Id} - " + 
                                      $"last used No: {item.Select(p => p.No).Max() +1}");
                }
            }
        }
    
        /// <summary>
        /// Should reside in a separate file
        /// </summary>
        public class PanelItem 
        {
            public int Id { get; set; }
            public int No { get; set; }
            public string Ref { get; set; }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, June 30, 2018 11:57 AM
    Moderator
  • Sorry, it's absolutly unclear what you mean by "next free position".

     <PANEL no="3" state="0" finished="0">

    finished="0"> is the next free position.

    Background: If I reload the file, I  must start the next unfinished position.

    With best regards Markus

    Sunday, July 1, 2018 10:06 AM
  • Hello Karen,

    yes looks not bad, if I can read my XML directly in a dataset.

       Inside dataset it is not depended from element or attribute? 

        Column name is the same as element, now it can be also a attribute, right?

    Can you show me, how I  can do it, if I want the first free unfinished number?

    <PANEL no="3" state="0" finished="0">

    With best regards Markus


    Sunday, July 1, 2018 11:31 AM
  • Hello Karen,

    yes looks not bad, if I can read my XML directly in a dataset.

       Inside dataset it is not depended from element or attribute? 

        Column name is the same as element, now it can be also a attribute, right?

    Can you show me, how I  can do it, if I want the first free unfinished number?

    <PANEL no="3" state="0" finished="0">

    With best regards Markus


    Here you go

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.IO;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                Shown += Form1_Shown;
            }
    
            private void Form1_Shown(object sender, EventArgs e)
            {
                var ds = new DataSet();
                var fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "File1.xml");
                ds.ReadXml(fileName);
    
                var panelData = ds.Tables["PANEL"]
                    .AsEnumerable()
                    .Select(row => new PanelData()
                    {
                        Id = row.Field<int>("PANEL_id") + 1,
                        Finished = row.Field<string>("Finished")
                    } );
    
    
                var result = ds.Tables["INDEX"]
                    .AsEnumerable()
                    .Select(row => new PanelItem
                    {
                        Id = row.Field<int>("PANEL_id") + 1,
                        No = Convert.ToInt32(row.Field<string>("no")),
                        Ref = row.Field<string>("ref1")
                    })
                    .ToList();
    
                List<List<PanelItem>> groupedList = result
                    .GroupBy(u => u.Id)
                    .Select(grp => grp.ToList())
                    .ToList();
    
                int id;
                string finished;
                foreach (var item in groupedList)
                {
                    // ReSharper disable once PossibleNullReferenceException
                    id = item.FirstOrDefault().Id;
                    finished = panelData.FirstOrDefault(curr => curr.Id == id).Finished;
    
                    // ReSharper disable once PossibleNullReferenceException
                    Console.WriteLine($"Id: {id} - " + 
                                      $"last used No: {item.Select(p => p.No).Max() +1} " + 
                                      $"Finished: {finished}");
                }
            }
        }
    
        /// <summary>
        /// Should reside in a separate file
        /// </summary>
        public class PanelItem 
        {
            public int Id { get; set; }
            public int No { get; set; }
            public string Ref { get; set; }
        }
    
        public class PanelData
        {
            public int Id { get; set; }
            public string Finished { get; set; }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, July 1, 2018 12:09 PM
    Moderator
  • Thanks for the quick response.

    I think, I can use it similar without dataset, isn't?

    So I have two possibilities.

    Markus


    Sunday, July 1, 2018 12:19 PM
  • Thanks for the quick response.

    I think, I can use it similar without dataset, isn't?

    So I have two possibilities.

    Markus


    I've been out all day.

    In regards to your question [I think, I can use it similar without dataset, isn't?], well there are many ways to access this data, I elected a simple and effective path to follow and that is all I'm presenting because it works and has no drawbacks for this task. 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, July 1, 2018 11:48 PM
    Moderator