0

I am writing at this point an application that will mainly display data. I've used EF to display data and have a method that pulls data to a datagridview. Based on this method, I would like to create a search for WZ documents using a textbox. Generally, it worked, but I know that both of these methods can be somehow transformed into shorter ones.

namespace Sito{

public partial class MainWindow : Window
{
    public object SqlMethods { get; private set; }

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        EntitiesSito db = new EntitiesSito();

        getdata();



    }

    private void filtertxbox_TextChanged(object sender, TextChangedEventArgs e)
    {
        string numberofWZ = filtertxbox.Text;


        if (string.IsNullOrEmpty(numberofWZ) )
        {
            getdata();
        }
        else
        {
            EntitiesSito db = new EntitiesSito();

            var query2 = (from a in db.WZ_DWS_SITO
                          where a.WZ.Contains(numberofWZ)
                          orderby a.WZ_DATA descending
                          group a by new { a.WZ, a.KUNNR, a.WZ_DATA } into grp
                          select new
                          {
                              grp.Key.WZ,
                              grp.Key.KUNNR,
                              grp.Key.WZ_DATA,
                              MATERIAL = grp.Count(),
                          }).ToList();

            datagridview.ItemsSource = query2;

        }
    }


    public void getdata()
    {

        EntitiesSito db = new EntitiesSito();

        var query = (from d in db.WZ_DWS_SITO
                     orderby d.WZ_DATA descending
                     group d by new { d.WZ, d.KUNNR, d.WZ_DATA } into grp
                     select new
                     {
                         grp.Key.WZ,
                         grp.Key.KUNNR,
                         grp.Key.WZ_DATA,
                         MATERIAL = grp.Count(),
                     }).ToList();

        datagridview.ItemsSource = query;

    }}}

How to improve this code for better ergonomy.

I am also asking for clarification of the issues related to writing a better code and what I can pay attention to in the future.

Błażej
  • 125
  • 2
  • 11
  • 3
    Fits better to: https://codereview.stackexchange.com/ – Tim Schmelter Dec 07 '18 at 11:08
  • Thanks, I didn't know that. – Błażej Dec 07 '18 at 11:16
  • I suggest two more simple, yet important things. 1. [Naming] (https://stackoverflow.com/a/1618325/6640527) , 2. Formatting [Order class items] (https://stackoverflow.com/a/310967/6640527) – Diego Osornio Dec 07 '18 at 12:24
  • @NetMage Feel free to recommend the OP post on CR but in the future, please don't use the existence of the Code Review site as a reason to close a question. Evaluate the request and use a reason like *too broad*, *primarily opinion-based*, etc. Then you can mention to the OP that it can be posted on Code Review if it is [on-topic](https://codereview.stackexchange.com/help/on-topic). Please see the section **What you should not do** in [this answer to _A guide to Code Review for Stack Overflow users_](https://codereview.meta.stackexchange.com/a/5778/120114) – Sᴀᴍ Onᴇᴌᴀ Dec 07 '18 at 18:27
  • @SᴀᴍOnᴇᴌᴀ Just because Stack Overflow's Off Topic option is so poorly written that even moderators disagree is no reason not to do the right thing. – NetMage Dec 07 '18 at 18:32
  • @NetMage Then in that case, [please flag for moderator intervention with a reason why you believe the migration would be useful.](https://meta.stackoverflow.com/a/266750/1575353) – Sᴀᴍ Onᴇᴌᴀ Dec 07 '18 at 19:24

0 Answers0