How can i return list looping select to monitor datagrid with linq?

How can i return list from looping select statements with linq. i need List<List<T>> but i dislike this method. how can i do that?

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        IStockMaterial stockMaterials = new StockProcedure();
        dataGridView1.DataSource = stockMaterials.StockMaterialList(2);
    }
}

interface IStockMaterial
{
   List<StockMaterial> StockMaterialList(int ItemID);
}

public class StockProcedure : IStockMaterial
{
    public List<StockMaterial> StockMaterialList(int ItemID)
    {
        using (StockProcedureDataContext stock = new StockProcedureDataContext())
        {
            List<StockMaterial> listStockMaterials = new List<StockMaterial>();
            try
            {
                 listStockMaterials = (from sm in stock.StockMaterials
                                       select (from s in stock.StockMaterials
                                       where (s.expirydate <= DateTime.Now &&
                                       s.expirydate.ToString() != "01/01/1900 00:00") &&
                                       s.chkexpirydate == true
                                       select s)).ToList();
            }
            catch (Exception e)
            {
                MessageBox.Show("Hata:" + e.Message);
            }
            return listStockMaterials;
        }
    }
}

Answers


Not sure where the requirement to return a List<List<T>> is coming from because your method signature is simple public List<StockMaterial>.

You could reduce all the above to:

return
  stock.StockMaterials
    .Where(s => 
       s.expirydate <= DateTime.Now && 
       s.expirydate.ToString() != "01/01/1900 00:00") && 
       s.chkexpirydate)
     .ToList();

I would also declare a constant for the Magic string.


Need Your Help

Create/edit Vagrant base box to pre-install cookbooks

ruby chef virtual-machine vagrant

My VM takes about 10 minutes to boot because of all the cookbooks it needs to install.

cannot find symbol class BluetoothPan

android networking bluetooth

I cannot import BluetoothPan class (import android.bluetooth.BluetoothPan; does not work). What can I do for importing BluetoothPan?