Tag: linq to sql

如何逐步建立一个Linq to Sql where子句?

我正在一个Parameters类中传递一组查询字符串参数,用于查询图像数据库。 每次调用时,一些参数可以为null。 所以在sql中我会建立像这样的查询 if (parameters.Value1 != null) { sql.Append(“sql_where_clause”); } if (parameters.Value2 != null) { sql.Append(“sql_where_clause”); } 我如何使用Linq做同样的事情?

在另一个表达式中评估C#表达式

我想在另一个中使用表达式: Expression<Func> f = x => x * x * 27 + blah … expression with x; Expression<Func> g = y => 3 + 8 * f.Compile()(y) * y * blah… expression with y and f(y); 这在发送到LINQ to SQL时不起作用,因为SQL不知道f.Compile()。 如何在不编译变量y情况下计算变量y上的表达式f ,但是仍然使用常规语法来定义g ? 我不想用一些不可读的Expression.Add / Expression.Multiply等语句来定义所有g 。 谢谢。

LINQ-to-SQL:存储过程返回单个标量值?

我正在使用LINQ-to-SQL来查询旧数据库的应用程序。 我需要调用一个存储过程,它选择一个整数值。 无法更改存储过程。 设计者使用此签名创建一个方法: private ISingleResult NextRowNumber([Parameter(DbType=”Int”)] System.Nullable increment, [Parameter(DbType=”Char(3)”)] string dataset) 我希望返回类型为int。 如何使用LINQ-to-SQL执行此操作?

使用guid主键忽略LINQ to SQL实体列名称属性

我正在使用LINQ to SQL(SQL Server 2005 SP3 x64)的简单实体类。 [Table( Name=”TBL_REGISTRATION” )] public sealed class Registration : IDataErrorInfo { [Column( Name = “TBL_REGISTRATION_PK”, IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert )] public Guid RegistrationID { get; private set; } /* other properties ommited for brevity */ } 这里只有两个有趣的事情: 类和属性名称与表名和列名不同 主键是Guid(uniqueidentifier) 这是表格的样子: create table dbo.TBL_REGISTRATION ( […]

Linq 2 Sql DateTime格式为字符串yyyy-MM-dd

基本上,我需要相当于T-SQL CONVERT(NVARCHAR(10), datevalue, 126) 我试过了: from t in ctx.table select t.Date.ToString(“yyyy-MM-dd”)但它抛出不支持的exception from t in ctx.table select “” + t.Date.Year + “-” + t.Date.Month + “-” + t.Date.Day但我不认为这是一个可用的解决方案,因为我可能需要能够改变格式。 我看到的唯一选择是使用Convert.ToString(t.Date, FormatProvider) ,但我需要一个格式提供程序,我不确定它是否有效 FormatProvider不起作用, String.Format不起作用( string.Format(“{0:yyyy-MM-dd}”, t.Date)抛出不支持的exception)。

LINQ to SQL – 没有可用的Add方法

我创建了一个LINQ to SQL datacontext,其中包含一个数据表。 我试图简单地在该表中插入一条新记录。 我遇到的问题是LINQ没有提供一个Add方法来传递新记录。 我看过无数的例子,其中有一个Add方法,但我似乎无法得到它。 我完全错过了某些东西还是其他东西? using (praetoriaTestDataContext db = new praetoriaTestDataContext()) { PageHit hit = new PageHit(); hit.DateViewed = DateTime.Now; hit.Page = “test”; db.PageHits.Add(hit); //Add method is not available! db.SubmitChanges(); } 谢谢!

将已编译的lambda表达式用于Average时的NotSupportedException

我试图回答这个问题,但失败了: 那么我们来看看原始查询: var result = db.Employees.GroupBy(x => x.Region) .Select(g => new { Region = g.Key, Avg = g.Average(x => x.BaseSalary)}); 工作良好。 现在我们想要动态决定平均值。 我尝试动态创建lambda的lambda: string property = “BaseSalary”; var parameter = Expression.Parameter(typeof(Employee)); var propAccess = Expression.PropertyOrField(parameter, property); var expression = (Expression<Func>)Expression.Lambda(propAccess, parameter); var lambda = expression.Compile(); 并使用它: var result = db.Employees.GroupBy(x => x.Region) .Select(g => new […]

如何从C#中的函数返回Generic.List

ASP.NET 3.5 C# 我正在使用Linq加入两张桌子。 表名是MapAssets和ExitPoint。 在数据库中他们与’有关系’有关 我正在我的BLL中编写一个函数来返回连接表 public List GetExitPointDetailsByProjectID(int iProjectID) { ctx = new CoreDBDataContext(); var exitPointDetails = from ma in ctx.MapAssets join ep in ctx.ExitPoints on ma.MapAssetID equals ep.MapAssetID where ma.ProjectID == iProjectID select new { //would like to have data from both tables here ctx.MapAssets, ctx.ExitPoints }; return exitPointDetails.ToList(); } 这显然不起作用。 我根本不知道该返回什么。 […]

WP8 linq-to-sql绑定问题上的LongListSelector

我正在构建我的第一个WP8应用程序,通过linq-to-sql连接到数据库,我想在LongListSelector中显示数据。 但是在Emulator中启动后没有任何东西出现在黑屏上。 请问哪里弄错了? 我用WCF连接 这是MainPage.xaml.cs using PhoneApp1.Resources; using PhoneApp1.ToursServiceReference1; namespace PhoneApp1 { public partial class MainPage : PhoneApplicationPage { // Constructor public MainPage() { InitializeComponent(); // Sample code to localize the ApplicationBar //BuildLocalizedApplicationBar(); } private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) { ToursServiceReference1.ToursService1Client serviceClient = new ToursServiceReference1.ToursService1Client(); serviceClient.GetAllKlientsCompleted += new EventHandler(serviceClient_GetAllKlientsCompleted); serviceClient.GetAllKlientsAsync(); } private void serviceClient_GetAllKlientsCompleted(object […]

linq-to-sql或linq在什么时候向数据库发送请求

我想让我的查询更好,但是无法找到一个资源,当查询被发送到数据库时。 DBContext db = new DBContext(); Order _order = (from o in db where o.OrderID == “qwerty-asdf-xcvb” select o).FirstOrDefault(); String _custName = _order.Customer.Name +” “+_order.Customer.Surname; _custName的赋值是否需要向数据库发出任何请求?