LINQ 帮助初学者选择多个表

发布于 2024-11-17 12:30:46 字数 1785 浏览 3 评论 0原文

我有以下存储库,其中包含三种返回数据库对象的方法,前两个查询工作正常,因为它们仅返回一个数据列表,但是由于第三个方法需要从两个表中进行选择,我对如何执行此操作有点困惑。

任何人都可以向我指出正确的方向,即如何编写 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; 使用系统文本; 使用 CustomerDatabase.Domain.Entities;

命名空间 CustomerDatabase.Domain.Abstract { 接口 ICustomersAndSitesM { IQueryable 客户 { get; } IQueryable CustomerSites { 获取; } }

}

I have the following repository that includes three methods for returning my db objects, the first two queries work fine as they simly return one list of data, however as the thrird method requires a selection from both tables im a bit confused with how to do this.

Can anyone kindly point me in the right direction as how to write a LINQ query to select from two related tables to pass into the last method (CustomerAndSites) in the repository shown below - the tables are related on a customer ID field,

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; }
    }

}

}

====Update

This is my ICustomersAndSitesM interface, where do i define the CustomersAndSitesMix, do i need to create that as a seperate entity? i have a view model in my UI project that contains the properties for both objects.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CustomerDatabase.Domain.Entities;

namespace CustomerDatabase.Domain.Abstract
{
interface ICustomersAndSitesM
{
IQueryable Customers { get; }
IQueryable CustomerSites { get; }
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

念三年u 2024-11-24 12:30:46

如果您的两个表实际上已通过外键正确链接,那么当您拉入父记录时,LINQ 也会拉入子记录。您需要在调试时检查 DataContext 返回的模型,并且通过足够的挖掘您应该能够看到任何链接的记录。例如,如果您有链接到客户的站点,则在您的客户域模型结果中,您应该看到一个名为“站点”的字段,它是链接到客户的站点域模型的列表。

If your two tables are actually properly linked with a foreign key, LINQ will pull in the child records as well when you pull in the parent records. You need to examine the model that your DataContext returns when you are debugging and you should be able to see any linked records with enough digging. For example, if you have Sites linked to Customers, in your Customer domain model result, you should see a field called "Sites" that is a list of site domain models linked to the customer.

甜宝宝 2024-11-24 12:30:46

我猜您实际上想要获得“CustomersAndSitesMix”,而不是返回“ICustomersAndSitesRepository”。如果是这样,你可以这样做:

public IQueryable<ICustomersAndSitesM> CustomerAndSites
{
    get
    {
        return from customer in customerTable
               join site in customerSitesTable
                    on customer.PLACEKEYHERE equals site.PLACEKEYHERE
               select new CustomersAndSitesMix(customer, site);
    }
}

Instead of returning an 'ICustomersAndSitesRepository', I guess you actually want to get an 'CustomersAndSitesMix'. If so, you could do this :

public IQueryable<ICustomersAndSitesM> CustomerAndSites
{
    get
    {
        return from customer in customerTable
               join site in customerSitesTable
                    on customer.PLACEKEYHERE equals site.PLACEKEYHERE
               select new CustomersAndSitesMix(customer, site);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文