我想在我的数据库中找到一个用户,搜索电子邮件和电话号码.但是,如果我使用List或IEnumerable,我会得到一个空的refence异常.如果我不使用任何这些,则抛出"不支持SQL ...".
我的方法:
public List<tblMember> getAllMembers()
{
return db.tblMembers.ToList();
}
private void confirmMembership(string email, int phoneNumber)
{
//var allMembers = db.tblMembers.AsEnumerable(); throws same exception
tblMember member = getAllMembers().FirstOrDefault(x => x.email.Equals(email, StringComparison.OrdinalIgnoreCase) && x.phoneNumber == phoneNumber); //This line throws exception, around email.Equals()
if (member != null)
{
member.isConfirmed = true;
db.SubmitChanges();
}
else
throw new Exception("Member not found");
}
Run Code Online (Sandbox Code Playgroud)
如果我执行这样的搜索,则不会抛出异常:
private void confirmMembership(string email, int phoneNumber)
{
//var allMembers = db.tblMembers.AsEnumerable(); throws same exception
tblMember member = getAllMembers().FirstOrDefault(x => x.email == …Run Code Online (Sandbox Code Playgroud) 目前,我需要创建一个报告程序,该程序在SQL数据库中的许多不同表上运行报告.多个不同的客户端需要此功能,但某些客户端拥有比其他客户端更大 我想知道的是,如果查询一段时间过长,是否有可能在一段时间后停止查询.
为了给出一些上下文,一些客户端具有超过200万行的表,尽管不同的客户端在同一个表中可能只有50k行.我希望能够运行20秒的查询,如果还没有完成,那么向用户返回一条消息,说结果集太大,报告需要在小时之外生成不希望在白天运行资源密集型操作.
我正在开发一个需要使用的WinForms项目Linq-To-Sql.我已经能够使用该SqlMetal工具创建我的DataContext ,并进行一些查询.但是现在我遇到了一个我无法解决的问题.
我想做一个LEFT OUTER JOIN如下:
MyDatabase db = new MyDatabase(...);
var query = from p in db.ParentTable
join t in db.ChildTable on new {A = p.child_ID, B = p.OtherID}
equals new {A = t.ID, B = t.OtherID} into j1
from c in j1.DefaultIfEmpty()
select new
{
...
};
Run Code Online (Sandbox Code Playgroud)
当我编写此查询时,在编译时会在join单词中引发错误:
join子句中某个表达式的类型不正确.调用'GroupJoin'时类型推断失败
我知道这个错误是由之间的比较造成p.child_ID而且t.ID因为p.child_ID是int?和t.ID是int.但是,我该如何解决这个问题呢?如何在LEFT OUTER JOIN没有此错误的情况下执行?
p.child_ID是int?因为这列被标记为 …
我有VB代码通过linq-to-sql调用复杂的存储过程.VB代码是否会在继续之前等待SP完成?或者它只是调用服务器来执行SP(然后无论SP执行多长时间都继续)?
Line 1 Do something
Line 2 myLinqToSQLDb.doStoredProcedure //will the code wait for this to finish before doing line 3?
Line 3 Do something else
Run Code Online (Sandbox Code Playgroud) 关于DEFERRED EXECUTION,以下评论是否正确?
1. var x = dc.myTables.Select(r=>r);//yes
2. var x = dc.myTables.Where(..).Select(r=>new {..});//yes
3. var x = dc.myTables.Where(..).Select(r=>new MyCustomClass {..});//no
Run Code Online (Sandbox Code Playgroud)
换句话说,我一直认为投射自定义类对象总是会导致急切的执行.但是我找不到支持/否认它的引用(虽然我看到的结果与它相矛盾,因此帖子)
我正在从表中选择一些数据,其中包含与表有关系A的列value和类型,B其中有一列coef包含(-1,0,1)
从AI中检索时想要将valuewith 乘以coef.
我在Linq to Sql中有这个查询
decimal Rewards = db.User.FirstOrDefault(x => x.FFUserID == UserID).TotalCommission;
Run Code Online (Sandbox Code Playgroud)
此查询未找到与userID匹配的记录,因此很明显这就是抛出异常的原因.我的问题是 - 我认为通过使用.FirstOrDefault()如果没有记录它将返回一个默认对象 - 也认为这个默认对象将具有默认值0.0Mfor TotalCommission .如果这不是它的工作方式那么这将是写这个的最好方法,希望它被设置0.0M为默认.
这是最好的吗?
decimal Rewards = db.User.FirstOrDefault(x => x.FFUserID == UserID)
.TotalCommission ?? 0.0M
Run Code Online (Sandbox Code Playgroud) 如果我想搜索 req_ser ,req_year .. ,so on fields
我做以下事情:
if (obj.RequestSerial != 0)
{
if (condition != "")
condition = condition + " AND ";
condition += " req_ser= " + obj.RequestSerial;
}
if (obj.RequestYear != 0)
{
if (condition != "")
condition = condition + " AND ";
condition += " req_year = " + obj.RequestYear;
}
Run Code Online (Sandbox Code Playgroud)
然后在我的select语句中,我执行以下操作:
SELECT * FROM .... WHERE ..... + condition
Run Code Online (Sandbox Code Playgroud)
如何使用LINQ TO SQL实体使用相同的概念进行搜索?
这是我的DBML Designer代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
public partial class DataClassesDataContext : System.Data.Linq.DataContext
{
private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
#region Extensibility Method Definitions
partial void OnCreated();
#endregion
public DataClassesDataContext(string connection) :
base(connection, mappingSource)
{
OnCreated();
}
public DataClassesDataContext(System.Data.IDbConnection connection) :
base(connection, mappingSource)
{
OnCreated();
}
public DataClassesDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public DataClassesDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{ …Run Code Online (Sandbox Code Playgroud) 让我们假设这段代码:
Classified classified = new Classified();
classified.Title = title;
classified.IsActive = true;
user.Classifieds.Add(classified);
dContext.SubmitChanges();
Response.Redirect(string.Format("/classifieds/post/?cid=", classified.Id));
Run Code Online (Sandbox Code Playgroud)
嗯,classifiedId没有价值,因为我被重定向到http://www.mysite.loc/classifieds/post/?cid=
任何人?
linq-to-sql ×10
c# ×9
linq ×7
asp.net ×3
.net ×2
compare ×1
repository ×1
search ×1
sql ×1
t-sql ×1