如何将简单的C#编码转换为Linq编码 [英] How to Convert Simple C# coding to Linq Coding

查看:102
本文介绍了如何将简单的C#编码转换为Linq编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

,,
我想转换此编码,这会给错误...任何人都可以帮助我..

Hi,,,
I want to Convert this coding this give error...any body help me..

DataTable GetMenuData(int id)
    {
        //using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CMSConnectionString"].ConnectionString))
        //{
        //    SqlDataAdapter dadCats = new SqlDataAdapter("SELECT * FROM MenuDetails where MenuId=1 and Status=1 and ParentID=" + id + "", con);
        //    DataSet dst = new DataSet();
        //    dadCats.Fill(dst);
        //    return dst.Tables[0];


        //}
        using (NKDataContext db = new NKDataContext())
        {
            var dt = db.GetMenuDetails().Where(x=>x.MenuID==1);

            return dt.CopyToDataTable();
        }
    }



请更正未注释的编码...



Please correct Uncommented Coding...

Thank''s in Advance.

推荐答案

"GetMenuDetails"是方法或表名吗?


你应该遵循这种方式...

Is "GetMenuDetails" a method or table name??


you should follow this way...

DataContext db = new DataContext();

var a= db.TableName.Where(Condition);
return a;




-Sagar Solanki




-Sagar Solanki


public static DataTable LINQToDataTable<t>(IEnumerable<t> varlist)
    {
        DataTable dtReturn = new DataTable();         
        PropertyInfo[] oProps = null;
        if (varlist == null) return dtReturn;
        foreach (T rec in varlist)
        {
            if (oProps == null)
            {
                oProps = ((Type)rec.GetType()).GetProperties();
                foreach (PropertyInfo pi in oProps)
                {
                    Type colType = pi.PropertyType;
                    if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
                    {
                        colType = colType.GetGenericArguments()[0];
                    }
                    dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
                }
            }
            DataRow dr = dtReturn.NewRow();
            foreach (PropertyInfo pi in oProps)
            {
                dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue
                (rec, null);
            }
            dtReturn.Rows.Add(dr);
        }
        return dtReturn;
    }</t></t>


这篇关于如何将简单的C#编码转换为Linq编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆