linqtosql实体类
‘壹’ LINQ执行存储过程的结果问题
象你这样的结构,首先,要生成一个表的实体类,如果你是自动加的LINQTOsql项目的话,在向导执行完的时候这个实体类应该自动生成了。否则就要自己动手生成。IQueryable<Users> abc = from bcf in the.Users where bcf.HasAd!="" select bcf;
            foreach (Users s in abc)
            {
                Users b = s;
                string c = b.HasAd;
            }我这里的USER就是我的实体类,具体代码如下,如果自己动手就按下面的代码,LINQ语句不用我解释了吧。[Table(Name="dbo.Users")]
 public partial class Users : INotifyPropertyChanging, INotifyPropertyChanged
 {
  
  private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
  
  private int _id;
  
  private string _UserName;
  
  private string _UserNameSpell;
  
  private string _ManagerName;
  
  private string _PassWord;
  
  private System.Nullable<int> _UserLevel;
  
  private System.Nullable<int> _UserJob;
  
  private string _UserPopedom;
  
  private string _Mob;
  
  private string _Tel;
  
  private System.Nullable<bool> _Sex;
  
  private System.Nullable<int> _TheCount;
  
  private System.Nullable<System.DateTime> _RegTime;
  
  private string _RegIP;
  
  private string _RegMAC;
  
  private System.Nullable<System.DateTime> _LoginTime;
  
  private string _LoginIP;
  
  private string _LoginMAC;
  
  private System.Nullable<System.DateTime> _LogoutTime;
  
  private string _LogoutIP;
  
  private string _LogoutMAC;
  
  private System.Nullable<int> _TheState;
  
  private string _HasAd;
  
  private System.Nullable<int> _SendTimeType;
  
  private System.Nullable<System.DateTime> _LastSendTime;
  
  private System.Nullable<System.DateTime> _LastFinish;
  
  private System.Nullable<byte> _MsgState;}这里只列出了字段,而且是私有的,这象肯定不行,你或者把这些字段改成公有,或者生成属性
‘贰’ LINQ to SQL 和一般的查询语句怎么什么区别
linq是面向对象的sql。也就是说,sql是向关系型数据库的查询,而linq实际上是对内存里的数据的查询。
虽然linq原来是对象查询,但经过ms的努力,可以通过表达式分析与实体到关系的映射(linq
to
sql),把linq转换为sql语句或是对xml的查询(linq
to
xml)。
因此,这种技术就成了对象到数据库记录的一个方便的映射、转化与操作的工具,你再也不必去去根据不同的情况用字符串拼接的办法生成sql,而是专心于对象模型的处理即可,你对于对象的修改最终都会被转换为对应的update,
insert,
delete等sql语句,在你submit时全部提交到数据库中。
综尔言之,linq
to
sql是一个数据库到对象结构的一个中间层,
他把对关系数据的管理转变为对象的操作,屏蔽了麻烦的sql,而且,还可以得到vs强大的智能感知功能的帮助。
‘叁’ DBContext实例中,表实体对象是怎么被加入的
系列的主角将一直是linqtosql.dbml,我们一般叫它数据对象关系映射,或者称为ORM,简单说就是数据库与对象之间作一个关系,这种关系我们称为Mapping,在LinqToSQL中,我们直接将某个数据库对象托到DBML文件中,这种关系映射就形成了,也就是说,以后想操作数据库直接操作DBML中的实体类型就可以了。
我们在使用某种ORM
‘肆’ C#→关于System.Data.Linq下的Table<TEntity> 泛型类 的问题
Table<TEntity>表示表格记录,它是一个泛型集合类,它的元素就是表格实体对象。它提供一组方法,对元素进行添加删除操作,并可以通过DataContext将这些操作保存到数据库。
表还是前面的那张表,在项目中添加了一个LINQ
to
SQL类。重点是InsertOnSubmit、DeleteOnSubmit等方法。
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
LINQ_to_SQL_Table
{
///
<summary>
///
操作单一表格的Table<TEntity>类
///
</summary>
class
Program
{static
void
Main(string[]
args){
//1.a.Attach附加实体
DataClasses1DataContext
dc1
=
new
DataClasses1DataContext();
tb_GuestInfo
guset
=
new
tb_GuestInfo()
{
Id=1,
Name
=
"DebugLZQ",
Age
=
35,
Tel
=
"138****8888"
};
dc1.tb_GuestInfo.Attach(guset);//这样的Attach仅仅附加实体,数据库没有更新
dc1.SubmitChanges();
//显示附加成功
foreach
(var
g
in
dc1.tb_GuestInfo)
{Console.WriteLine("{0}
{1}
{2}
{3}",
g.Id,
g.Name,
g.Age,
g.Tel);
}
Console.WriteLine("---------");
//显示数据库没有更新
DataClasses1DataContext
dc2
=
new
DataClasses1DataContext();
foreach
(var
g
in
dc2.tb_GuestInfo)
{Console.WriteLine("{0}
{1}
{2}
{3}",
g.Id,
g.Name,
g.Age,
g.Tel);
}
Console.WriteLine("------------------------");
Console.ReadKey();
//2.InsertOnSubmit
dc2.tb_GuestInfo.InsertOnSubmit(guset);
dc2.SubmitChanges();
foreach
(var
g
in
dc2.tb_GuestInfo)
{Console.WriteLine("{0}
{1}
{2}
{3}",
g.Id,
g.Name,
g.Age,
g.Tel);
}
Console.WriteLine("------------------------");
Console.ReadKey();
//2b.InsertAllOnSubmit
插入集合
List<tb_GuestInfo>
lst
=
new
List<tb_GuestInfo>()
{new
tb_GuestInfo(){
Name="AA",
Age=25,Tel="133****3333"},new
tb_GuestInfo(){
Name="BB",
Age=25,Tel="135****5555"}
};
dc2.tb_GuestInfo.InsertAllOnSubmit(lst);
dc2.SubmitChanges();
foreach
(var
g
in
dc2.tb_GuestInfo)
{Console.WriteLine("{0}
{1}
{2}
{3}",
g.Id,
g.Name,
g.Age,
g.Tel);
}
Console.WriteLine("------------------------");
Console.ReadKey();
//
//3.DeleteOnSubmit
tb_GuestInfo
entity
=
(from
g
in
dc2.tb_GuestInfo
where
g.Name
==
"AA"
select
g).Single();
dc2.tb_GuestInfo.DeleteOnSubmit(entity);//
dc2.SubmitChanges();
foreach
(var
g
in
dc2.tb_GuestInfo)
{Console.WriteLine("{0}
{1}
{2}
{3}",
g.Id,
g.Name,
g.Age,
g.Tel);
}
Console.WriteLine("------------------------");
Console.ReadKey();
//3b.DeleteAllOnSubmit
IEnumerable<tb_GuestInfo>
entitys
=
from
g
in
dc2.tb_GuestInfowhere
g.Name
==
"AA"
||
g.Name
==
"BB"select
g;
dc2.tb_GuestInfo.DeleteAllOnSubmit(entitys);
dc2.SubmitChanges();
foreach
(var
g
in
dc2.tb_GuestInfo)
{Console.WriteLine("{0}
{1}
{2}
{3}",
g.Id,
g.Name,
g.Age,
g.Tel);
}
Console.WriteLine("------------------------");
Console.ReadKey();
}
}
}
程序运行结果如下:

‘伍’ 求助java中sql语句中in条件如何参数化
应该是EntityFramework(实体框架)、LinqToSQL或者Lamda表达式,关键是Linq。下面我给你点示例代码你看是不是。public void LinqToSqlGroupBy04() { var q = from p in db.Procts group p by
‘陆’ C# 实体框架(实体数据模型 ) 更新数据有类似SqlDataAdapter Fill的方法吗
使用linqtosql 或者 EF 可以直接提交数据。savechange()
‘柒’ linq to sql类怎么用
打开VS2010新建控制台应用程序,然后添加LINQ
to
SQL
Class,命名为DbApp.dbml,新建dbml文件之后,可以打开server
explorer,建立数据库连接,并将我们新建的表拖到dbml文件中,
2.
可以通过点击dbml文件空白处,按F4显示dbml属性,可以修改Context和生成实体的命名空间
3.
到现在为止VS2010通过工具为我们创建好了数据表对应实体类和数据表操作添,改,删的方法,现在开始实践
‘捌’ 关于LINQforNwind的示例
给你一个LinqToEF  的例子吧 希望能帮到你
配置文件(连接字符串):
<connectionStrings>
    <add name="TempConStr" connectionString="Data Source = Address;DataBase = Temp;User Id = ??;Password =?? " /> --这个是LinqToSql 的链接字符串
    <add name="TempEntities" connectionString="metadata=res://*/Models.Temp.csdl|res://*/Models.Temp.ssdl|res://*/Models.Temp.msl;provider=System.Data.SqlClient;provider connection string="data source=Address;initial catalog=Temp;user id=??;Password = woshiwei1;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />  --这个是LinqToEF 的链接字符串
--数据模型  --可以用VS 自动生成
using System;
using System.Data.Linq.Mapping;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Data.EntityClient;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Runtime.Serialization;
[assembly: EdmSchemaAttribute()]
#region EDM 关系源元数据
[assembly: EdmRelationshipAttribute("TempModel", "FK_Purchase_Customers", "Customer", System.Data.Metadata.Edm.RelationshipMultiplicity.One, typeof(SimpleMvc.Models.Customer), "Purchase", System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(SimpleMvc.Models.Purchase), true)]
#endregion
namespace SimpleMvc.Models
{
    #region 上下文
    
    /// <summary>
    /// 没有元数据文档可用。
    /// </summary>
    public partial class TempEntities1 : ObjectContext
    {
        #region 构造函数
    
        /// <summary>
        /// 请使用应用程序配置文件的“TempEntities1”部分中的连接字符串初始化新 TempEntities1 对象。
        /// </summary>
        public TempEntities1() : base("name=TempEntities1", "TempEntities1")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }
    
        /// <summary>
        /// 初始化新的 TempEntities1 对象。
        /// </summary>
        public TempEntities1(string connectionString) : base(connectionString, "TempEntities1")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }
    
        /// <summary>
        /// 初始化新的 TempEntities1 对象。
        /// </summary>
        public TempEntities1(EntityConnection connection) : base(connection, "TempEntities1")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }
    
        #endregion
    
        #region 分部方法
    
        partial void OnContextCreated();
    
        #endregion
    
        #region ObjectSet 属性
    
        /// <summary>
        /// 没有元数据文档可用。
        /// </summary>
        public ObjectSet<Customer> Customer
        {
            get
            {
                if ((_Customer == null))
                {
                    _Customer = base.CreateObjectSet<Customer>("Customer");
                }
                return _Customer;
            }
        }
        private ObjectSet<Customer> _Customer;
    
        /// <summary>
        /// 没有元数据文档可用。
        /// </summary>
        public ObjectSet<Purchase> Purchase
        {
            get
            {
                if ((_Purchase == null))
                {
                    _Purchase = base.CreateObjectSet<Purchase>("Purchase");
                }
                return _Purchase;
            }
        }
        private ObjectSet<Purchase> _Purchase;
        #endregion
        #region AddTo 方法
    
        /// <summary>
        /// 用于向 Customer EntitySet 添加新对象的方法,已弃用。请考虑改用关联的 ObjectSet<T> 属性的 .Add 方法。
        /// </summary>
        public void AddToCustomer(Customer customer)
        {
            base.AddObject("Customer", customer);
        }
    
        /// <summary>
        /// 用于向 Purchase EntitySet 添加新对象的方法,已弃用。请考虑改用关联的 ObjectSet<T> 属性的 .Add 方法。
        /// </summary>
        public void AddToPurchase(Purchase purchase)
        {
            base.AddObject("Purchase", purchase);
        }
        #endregion
    }
#endregion
    
    #region 实体
    
    /// <summary>
    /// 没有元数据文档可用。
    /// </summary>
    [EdmEntityTypeAttribute(NamespaceName="TempModel", Name="Customer")]
    [Serializable()]
    [DataContractAttribute(IsReference=true)]
    [Table]
    public partial class Customer : EntityObject
    {
        #region 工厂方法
    
        /// <summary>
        /// 创建新的 Customer 对象。
        /// </summary>
        /// <param name="id">Id 属性的初始值。</param>
        /// <param name="name">Name 属性的初始值。</param>
        public static Customer CreateCustomer(global::System.Int32 id, global::System.String name)
        {
            Customer customer = new Customer();
            customer.Id = id;
            customer.Name = name;
            return customer;
        }
        #endregion
        #region 基元属性
    
        /// <summary>
        /// 没有元数据文档可用。
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
        [DataMemberAttribute()]
        [Column]
        public global::System.Int32 Id
        {
            get
            {
                return _Id;
            }
            set
            {
                if (_Id != value)
                {
                    OnIdChanging(value);
                    ReportPropertyChanging("Id");
                    _Id = StructuralObject.SetValidValue(value);
                    ReportPropertyChanged("Id");
                    OnIdChanged();
                }
            }
        }
        private global::System.Int32 _Id;
        partial void OnIdChanging(global::System.Int32 value);
        partial void OnIdChanged();
    
        /// <summary>
        /// 没有元数据文档可用。
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
        [DataMemberAttribute()]
        [Column]
        public global::System.String Name
        {
            get
            {
                return _Name;
            }
            set
            {
                OnNameChanging(value);
                ReportPropertyChanging("Name");
                _Name = StructuralObject.SetValidValue(value, false);
                ReportPropertyChanged("Name");
                OnNameChanged();
            }
        }
        private global::System.String _Name;
        partial void OnNameChanging(global::System.String value);
        partial void OnNameChanged();
        #endregion
    
        #region 导航属性
    
        /// <summary>
        /// 没有元数据文档可用。
        /// </summary>
        [XmlIgnoreAttribute()]
        [SoapIgnoreAttribute()]
        [DataMemberAttribute()]
        [("TempModel", "FK_Purchase_Customers", "Purchase")]
        public EntityCollection<Purchase> Purchase
        {
            get
            {
                return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<Purchase>("TempModel.FK_Purchase_Customers", "Purchase");
            }
            set
            {
                if ((value != null))
                {
                    ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<Purchase>("TempModel.FK_Purchase_Customers", "Purchase", value);
                }
            }
        }
        #endregion
    }
    
    /// <summary>
    /// 没有元数据文档可用。
    /// </summary>
    [EdmEntityTypeAttribute(NamespaceName="TempModel", Name="Purchase")]
    [Serializable()]
    [DataContractAttribute(IsReference=true)]
    public partial class Purchase : EntityObject
    {
        #region 工厂方法
    
        /// <summary>
        /// 创建新的 Purchase 对象。
        /// </summary>
        /// <param name="id">Id 属性的初始值。</param>
        /// <param name="customerId">CustomerId 属性的初始值。</param>
        /// <param name="description">Description 属性的初始值。</param>
        /// <param name="price">Price 属性的初始值。</param>
        public static Purchase CreatePurchase(global::System.Int32 id, global::System.Int32 customerId, global::System.String description, global::System.Decimal price)
        {
            Purchase purchase = new Purchase();
            purchase.Id = id;
            purchase.CustomerId = customerId;
            purchase.Description = description;
            purchase.Price = price;
            return purchase;
        }
        #endregion
        #region 基元属性
    
        /// <summary>
        /// 没有元数据文档可用。
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.Int32 Id
        {
            get
            {
                return _Id;
            }
            set
            {
                if (_Id != value)
                {
                    OnIdChanging(value);
                    ReportPropertyChanging("Id");
                    _Id = StructuralObject.SetValidValue(value);
                    ReportPropertyChanged("Id");
                    OnIdChanged();
                }
            }
        }
        private global::System.Int32 _Id;
        partial void OnIdChanging(global::System.Int32 value);
        partial void OnIdChanged();
    
        /// <summary>
        /// 没有元数据文档可用。
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.Int32 CustomerId
        {
            get
            {
                return _CustomerId;
            }
            set
            {
                OnCustomerIdChanging(value);
                ReportPropertyChanging("CustomerId");
                _CustomerId = StructuralObject.SetValidValue(value);
                ReportPropertyChanged("CustomerId");
                OnCustomerIdChanged();
            }
        }
        private global::System.Int32 _CustomerId;
        partial void OnCustomerIdChanging(global::System.Int32 value);
        partial void OnCustomerIdChanged();
    
        /// <summary>
        /// 没有元数据文档可用。
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.String Description
        {
            get
            {
                return _Description;
            }
            set
            {
                OnDescriptionChanging(value);
                ReportPropertyChanging("Description");
                _Description = StructuralObject.SetValidValue(value, false);
                ReportPropertyChanged("Description");
                OnDescriptionChanged();
            }
        }
        private global::System.String _Description;
        partial void OnDescriptionChanging(global::System.String value);
        partial void OnDescriptionChanged();
    
        /// <summary>
        /// 没有元数据文档可用。
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.Decimal Price
        {
            get
            {
                return _Price;
            }
            set
            {
                OnPriceChanging(value);
                ReportPropertyChanging("Price");
                _Price = StructuralObject.SetValidValue(value);
                ReportPropertyChanged("Price");
                OnPriceChanged();
            }
        }
        private global::System.Decimal _Price;
        partial void OnPriceChanging(global::System.Decimal value);
        partial void OnPriceChanged();
        #endregion
    
        #region 导航属性
    
        /// <summary>
        /// 没有元数据文档可用。
        /// </summary>
        [XmlIgnoreAttribute()]
        [SoapIgnoreAttribute()]
        [DataMemberAttribute()]
        [("TempModel", "FK_Purchase_Customers", "Customer")]
        public Customer Customer
        {
            get
            {
                return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<Customer>("TempModel.FK_Purchase_Customers", "Customer").Value;
            }
            set
            {
                ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<Customer>("TempModel.FK_Purchase_Customers", "Customer").Value = value;
            }
        }
        /// <summary>
        /// 没有元数据文档可用。
        /// </summary>
        [BrowsableAttribute(false)]
        [DataMemberAttribute()]
        public EntityReference<Customer> CustomerReference
        {
            get
            {
                return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<Customer>("TempModel.FK_Purchase_Customers", "Customer");
            }
            set
            {
                if ((value != null))
                {
                    ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedReference<Customer>("TempModel.FK_Purchase_Customers", "Customer", value);
                }
            }
        }
        #endregion
    }
--封装强类型的 ObjectContext 用于操作对应的模型.
 public class BaseDataContext : DataContext
    {
        public BaseDataContext(string fileOrServerOrConnection) : base(fileOrServerOrConnection)
        {
        }
        public BaseDataContext(string fileOrServerOrConnection, MappingSource mapping)
            : base(fileOrServerOrConnection, mapping)
        {
        }
        public Table<Customer> Customers
        {
            get { return GetTable<Customer>(); }
        }
        public Table<Purchase> Purchases
        {
            get { return GetTable<Purchase>(); }
        }
    }
--这里是调用
var dataContext =
                new BaseObjectContext(ConfigurationManager.ConnectionStrings["TempEntities"].ConnectionString);
var query = from c in dataContext.Customers
                        select new
                                   {
                                       c.Name,
                                       Purchases = from p in dataContext.Purchases
                                                   where p.CustomerId == c.Id && p.Price > 1000
                                                   select new {p.Description, p.Price}
                                   };
‘玖’ 怎么写关于“IN”操作符的SQL语句的参数化命令
根据你说的描述,应该是EntityFramework(实体框架)、LinqToSQL或者Lamda表达式,关键是Linq。下面我给你点示例代码你看是不是。public void LinqToSqlGroupBy04() { var q = from p in db.Procts group p by p.CategoryID into g select new { g.Key, AveragePrice = g.Average(p => p.UnitPrice) }; ObjectDumper.Write(q, 1);}public void LinqToSqlWhere05() { var q = db.Procts.Where(p=>p.UnitPrice > 10m).Where(p=>p.Discontinued); ObjectDumper.Write(q, 0);}
‘拾’ 数据表关系比较多的数据库 三层架构应该怎么设计
实体类,也就是把数据库表的字段映射为你的对象的各个属性如你A表有,id,name,password三个属性你Model里面新建Aclass给他ID,Name,Password三个属性然后做和数据表字段映射LINQTOSQL的时候就会自动读取映射的字段并转换为实体类的属性值
