【发布时间】:2011-06-29 15:38:36
【问题描述】:
我有以下存储库,其中包括用于返回我的 db 对象的三种方法,前两个查询工作正常,因为它们只返回一个数据列表,但是由于第三种方法需要从两个表中进行选择,我对如何这样做。
任何人都可以指出正确的方向,即如何编写 LINQ 查询以从两个相关表中进行选择,以传递到下面显示的存储库中的最后一个方法 (CustomerAndSites) - 这些表与客户 ID 字段相关,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CustomerDatabase.Domain.Abstract;
using CustomerDatabase.Domain.Concrete;
using CustomerDatabase.Domain.Entities;
using System.Data.Linq.Mapping;
using System.Data.Linq;
using System.Web.Mvc;
namespace CustomerDatabase.Domain.Concrete
{
class SqlCustomersAndSitesRepository : ICustomersAndSitesRepository
{
public Table<CustomerSite> customerSitesTable;
public Table<Customer> customerTable;
public SqlCustomersAndSitesRepository(string connectionString)
{
customerSitesTable = (new DataContext(connectionString)).GetTable<CustomerSite>();
customerTable = (new DataContext(connectionString)).GetTable<Customer>();
}
public IQueryable<CustomerSite> CustomerSites
{
get { return customerSitesTable; }
}
public IQueryable<Customer> Customers
{
get { return customerTable; }
}
public IQueryable <ICustomersAndSitesRepository> CustomerAndSites
{
get { return CustomerAndSites; }
}
}
}
====更新
这是我的 ICustomersAndSitesM 接口,我在哪里定义 CustomersAndSitesMix,我需要将其创建为单独的实体吗?我的 UI 项目中有一个视图模型,其中包含两个对象的属性。
使用系统; 使用 System.Collections.Generic; 使用 System.Linq; 使用 System.Text; 使用 CustomerDatabase.Domain.Entities;
命名空间 CustomerDatabase.Domain.Abstract { 接口 ICustomersAndSitesM { 可查询的客户{得到; } IQueryable CustomerSites { 得到; } }
}
【问题讨论】:
-
为什么要返回 ICustomersAndSitesRepository 的 IQueryable?这对我没有任何意义
-
你在构造函数中做什么? !您是否运行/测试过这些代码?
标签: c# linq asp.net-mvc-2 repository viewmodel