Tag: linq to sql

Linq存储过程问题 – 返回int

我试图用Linq调用一个存储过程,存储过程在SQL中返回值很好,但当我将它拖到我的DBML文件并尝试从我的代码调用它时它返回 无法找到源类型“int”的查询模式的实现。 找不到“选择”。 我查看了其他线程和其他存储过程,由于某些原因而不是使用ISingleResult这是不同的,我似乎也无法更改返回类型。 这是背后的DBML代码 [global::System.Data.Linq.Mapping.FunctionAttribute(Name=”dbo.displayDetails”)] public int displayDetails([global::System.Data.Linq.Mapping.ParameterAttribute(DbType=”VarChar(1)”)] string sex, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType=”Int”)] System.Nullable day, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType=”Int”)] System.Nullable month, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType=”Int”)] System.Nullable year, [global::System.Data.Linq.Mapping.ParameterAttribute(Name=”PostCode”, DbType=”VarChar(10)”)] string postCode, [global::System.Data.Linq.Mapping.ParameterAttribute(Name=”AppTime”, DbType=”DateTime”)] System.Nullable appTime, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType=”Int”)] System.Nullable filter) { IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), sex, day, month, year, postCode, appTime, filter); return ((int)(result.ReturnValue)); } 我的页面代码正在收到错误 var person = from p in db.displayDetails(sex.ToString(), […]

LINQ和参考列表问题

我有以下代码与LINQ var q = (from web in DataContext.Webs select web); List k1 = new List() { 1, 2 }; List k2=new List() { 16, 17 }; q = q.Where(web => DataContext.WebTechMaps.Any(t => t.WebsiteId == web.WebsiteId && k1.Contains(t.TechId))); System.Diagnostics.Debug.WriteLine(q.Count()); k1 = k2; q = q.Where(web => DataContext.WebTechMaps.Any(t => t.WebsiteId == web.WebsiteId && k1.Contains(t.TechId))); System.Diagnostics.Debug.WriteLine(q.Count()); 这是它生成的查询。 第一次执行计数。 […]

LINQ:如何在linq中动态使用ORDER BY,但仅限于变量不是string.empty或null

我正在使用LINQ2SQL,它的工作非常好。 但是,根据C#中变量类型字符串的值,我需要在查询中使用“Order By”或不使用“order by”。 如果C#字符串不为null或为空,那么我想对字符串变量的内容进行“排序依据”。 如果C#字符串为空或为null,那么我不包含顺序。 是否可以编写这种查询?

LINQ to SQL – 1对1关联的问题

在L2S设计器中,我删除了一个表和一个视图。 我尝试在主键上添加2之间的关联。 这应该是一对一的关系,所以我设置了关联的基数属性。 但是,编码时我无法访问子属性。 有什么建议? 编辑 我刚刚在sql server中创建了一个包含我想要的字段的视图,并将其放入L2S中。 更简单。

在LINQ to SQL中查找或创建对象的通用方法?

在我的LINQ to SQL代码中,我经常需要“查找或创建”一个实体: var invoiceDb = ctx.Invoices.FirstOrDefault(a => a.InvoicerId == InvoicerId && a.Number == invoiceNumber); if (invoiceDb == null) { invoiceDb = new Invoice(); invoiceDb.Number = invoiceNumber; ctx.Invoices.InsertOnSubmit(invoiceDb); } 我想把它变成一个通用的方法……有什么好主意吗?

如何在存储库中正确处理Linq到SQL DataContext?

在Rob Conery风格的ASP.NET MVC应用程序中,您通常有一个存储库: public class CustomerRepository { DataContext dc = new DataContext(); public IQueryable AllCustomers() { return db.Customers; } public Customer GetCustomer(int customerID) { return db.Customers.FirstOrDefault(c => c.CustomerID = customerID); } } 和一个控制器: public class CustomerController: Controller { CustomerRepository _repository; public ActionResult Index() { var data = _repository.AllCustomers(); return view(“Index”, data); } public ActionResult Details(int […]

使用Moq对LINQ to SQL CRUD操作进行unit testing

我已经浏览了其他问题,但没有什么能与我正在寻找的东西相提并论……主要是因为我并不是100%肯定我正在寻找什么! 基本上我目前正在开发一个新项目,我已经为数据库实体创建了抽象层,并将DAC设置为存储库。 我想用Mock对象对它进行unit testing,但是我已经用CRUD(特别是C)操作打了一个知识墙,我的unit testing类: [TestClass] public class RepositoryTest { private Mock _repository; private IList _personStore; [TestInitialize()] public void Initialize() { _personStore= new List() { new Person() { Name = “Paul” }, new Person() { Name = “John” }, new Person() { Name = “Bob” }, new Person() { Name = “Bill” }, }; _repository […]

Linq to SQL datacontext不更新外键关系

我正在针对使用L2S的存储库编写数据库测试。 在我的数据库中,我有一个Manifest实体和一个AllocatedTransaction实体。 AllocatedTransaction实体具有Manifest id的外键。 DDL看起来像这样: Manifest: Id – int – identity AllocateTransaction: Id – int – identity Quantity – int ManifestId – FK to Manifest 在我的测试中,我正在检查AllocatedTransactions是否与Manifest一起回来。 测试看起来像这样: [TestMethod] public void FindByIdTest() { using (data.EZTracDataContext dataContext = new data.EZTracDataContext()) { using (new TransactionScope()) { data.Manifest manifest = _testDataProvider.AddManifest(dataContext); data.AllocatedTransaction allocatedTransaction = _testDataProvider.AddEligibilityAllocated(dataContext, 5, manifest.Id); ManifestRepository repository […]

“Where”子句中的动态表达式 – Linq to SQL

我是LINQ的新手,所以我希望这不是一个愚蠢的问题: 我在数据网格中有一个包含大量内容的表格,我希望用户能够通过使用网格上方的一些combobox来过滤网格[如搜索栏] 我创建了一个方法,它接受combobox中的文本,并将其放在“Where”子句中: public void find() { string disName; string statusName; disName = RMcmbDis.Text; //This Get the first string to filter statusName = RMcmbStatus.Text; // this get the second string to filter //在这里,我收集了我需要的所有数据 var allCNT = from x in cntDB.releases join dis in cntDB.disciplines on x.discipline equals dis.discipline_id join btch in cntDB.batches on x.batch_num equals […]

C#将Table 添加到现有的DataContext实例中

是否有可能将Table动态添加到LinqToSQL中的现有DataContext Insance? 这是我的情况:我有几个使用[Test]属性声明为Table的分类,我想允许用户在运行时创建相应的SQL Server表。 据我所知,如果我inheritanceDataContext类,我可以添加成员表客户; 在类中,它将在后端数据库中自动创建这样的表。 但是,问题是我是否可以在运行时将Table添加到DataContext类,这有助于我创建对应于T的对应的SQL Server表。