none
What is the TableName and ColumnName at my linq ?

    Question

  • Hello

    For example

    sqlquery="Select abc From TableX";

    DataTable dt = ......

    So dt.TableName is TableX and dt[0].Columnname is abc.

    I have below linq. I couldnt get it as DataTable. So i couldnt learn TableName and ColumnName.

    What is the TableName and Column Names at below query if i get it as DataTable.

    I will use it at datagridstyle.mappingname and columnstyle.mappingname. I couldnt apply styles for my datagrid.

    I get below at datagrid1.datasource= .....; And i tried DataTable dt =(DataTable)DataGrid1.datasource. But it give me error.

    public List<ViewRotaTakip> TabloDoldur(string calisanID, string konumAdi)
            {
                using (abcDataContext abcbaglanti = new abcDataContext(Properties.Settings.Default.abcConnectionString))
                {
                    if (!string.IsNullOrEmpty(konumAdi))
                    {
    
                        var tablo = (from u in abcbaglanti.vUYUretimRotaTakips
                                     join i in abcbaglanti.UYKonumIslemTurus on
                                     u.IslemTuruID equals i.IslemTuruID
                                     orderby u.Durum descending, u.PlanlananBitisTarihi ascending
                                     where ((i.KonumID == abcbaglanti.UYKonums.Single(q => q.KonumKoduAdi == konumAdi).KonumID) && ((u.RotaDurumu == 2) && (u.CalisanID == int.Parse(calisanID)) || u.RotaDurumu == 1 || u.RotaDurumu == 3 || u.RotaDurumu == 7))
                                     select new ViewRotaTakip
                                     {
                                         OperasyonKodu = u.OperasyonuKodu,
                                         SiparisID = u.SiparisID.GetValueOrDefault(),
                                         IsEmriKodu = u.IsEmriKodu,
                                         Miktar = u.PlanlananGirdi.HasValue ? (decimal)u.PlanlananGirdi : u.UretimMiktari.GetValueOrDefault(),
                                         GerceklesenMiktar = u.GerceklesenCikti.GetValueOrDefault(),
                                         FireMiktar = u.Fire.GetValueOrDefault(),
                                         ToplamBitenFire = Convert.ToInt64(u.PlanlananGirdi.HasValue ? (decimal)u.PlanlananGirdi : u.UretimMiktari.GetValueOrDefault()).ToString() + " - " + u.GerceklesenCikti.GetValueOrDefault().ToString() + " - " + u.Fire.GetValueOrDefault().ToString(),
                                         UretimPlaniID = u.UretimPlaniID.GetValueOrDefault(),
                                         PlanlananBaslamaTarihi = u.PlanlananBaslamaTarihi.HasValue ? u.PlanlananBaslamaTarihi.Value : DateTime.Now,
                                         PlanlananBitisTarihi = u.PlanlananBaslamaTarihi.HasValue ? u.PlanlananBitisTarihi.Value : DateTime.Now,
                                         UrunKodu = u.UrunKodu,
                                         UrunAdi = u.Adi,
                                         UrunKoduAdi = u.UrunKoduAdi,
                                         UrunKategorisiID = (int)u.UrunKategorisiID,
                                        ........................                                 }).ToList<ViewRotaTakip>();
    
    
    

    Wednesday, December 11, 2013 2:08 PM

Answers

  • Hello,

    >>I have below linq. I couldnt get it as DataTable. So i couldnt learn TableName and ColumnName.

    Yes, we cannot get the result as DataTable directly. The result of linq query provided by you will be the List<ViewRotaTakip> type. Of course, it does not have the tablename and columnname.

    We need to create a function that will do the conversion from the List<T> type to datatable. One way is like below:

    var query = from c in db.something
    
                where c.othersomething == "onlyyouknow"
    
                orderby c.othersomething
    
                select new { NewObject = c.othersomething };
    
    
    DataTable MyDataTable = new DataTable();
    
    myDataTable.Columns.Add(
    
        new DataColumn()
    
        {
    
            DataType = System.Type.GetType("System.String"),//or other type
    
            ColumnName = "Name"      //or other column name
    
        }
    
    );
    
    
    foreach (var element in query)
    
    {
    
        var row = MyDataTable.NewRow();
    
        row["Name"] = element.NewObject;
    
        myDataTable.Rows.Add(row);
    
    }
    
     Regards.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by MSDN.CSharp Saturday, December 14, 2013 3:35 PM
    Thursday, December 12, 2013 3:42 AM
    Moderator