如何从使用LINQ到SQL的方法返回查询结果 [英] How to return query results from method that uses LINQ to SQL

查看:96
本文介绍了如何从使用LINQ到SQL的方法返回查询结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在使用的代码,我对LINQ还是有点陌生​​,所以这是一项正在进行的工作.具体来说,我想从该查询中获取结果(大约7列字符串,整数和日期时间),并将其返回给调用包含此LINQ to SQL查询的方法的方法.一个简单的代码示例将非常有帮助.

Here is the code I'm working with, I'm still a bit new to LINQ, so this is a work in progress. Specifically, I'd like to get my results from this query (about 7 columns of strings, ints, and datetime), and return them to the method that called the method containing this LINQ to SQL query. A simple code example would be super helpful.

using (ormDataContext context = new ormDataContext(connStr))
{
    var electionInfo = from t1 in context.elections
               join t2 in context.election_status
               on t1.statusID equals t2.statusID
               select new { t1, t2 };
}

(在这种情况下,我的查询返回的是2个表的所有内容,选举和选举状态.)

(In this case, my query is returning all the contents of 2 tables, election and election_status.)

推荐答案

具体地说,我想得到我的 此查询的结果(约7 字符串,整数和 日期时间),然后将其返回

Specifically, I'd like to get my results from this query (about 7 columns of strings, ints, and datetime), and return them

您好,查询遇到的问题是您正在创建匿名类型.您无法从方法中返回匿名类型,因此这将给您带来麻烦.

Hi, the problem you've got with your query is that you're creating an anonymous type. You cannot return an anonymous type from a method, so this is where you're going to have trouble.

您需要做的是创建一个包装器"类型,该类型可以进行选举和选举状态,然后返回它们.

What you will need to do is to create a "wrapper" type that can take an election and an election_status and then return those.

以下是我所谈论的内容的一个示例;如您所见,我声明了一个Tuple类.您将包装查询的方法将返回IEnumerable.

Here's a little sample of what I'm talking about; as you can see I declare a Tuple class. The method that you will wrap your query in returns an IEnumerable.

我希望这会有所帮助:-)

I hope this helps :-)

class Tuple
{
    Election election;
    Election_status election_status;

    public Tuple(Election election, Election_status election_status)
    {
        this.election = election;
        this.election_status = election_status;
    }
}

public IEnumerable<Tuple> getElections()
{
    IEnumerable<Tuple> result = null;

    using (ormDataContext context = new ormDataContext(connStr))
    {
        result = from t1 in context.elections
                 join t2 in context.election_status
                 on t1.statusID equals t2.statusID
                 select new Tuple(t1, t2);
    }
}

更新

根据NagaMensch的评论,达到预期结果的更好方法是使用内置的LINQ to SQL关联.

Following from NagaMensch's comments, a better way to achieve the desired result would be to use the built in LINQ to SQL associations.

如果转到实体图并单击工具箱,您将看到3个选项.类,关联和继承.我们要使用关联.

If you go to your entity diagram and click on toolbox, you will see 3 options. Class, Association and Inheritance. We want to use Association.

  • 单击关联",然后单击"ElectionStatus"实体,按住鼠标按钮不放,您可以在"Election"实体上画一条线.

  • Click on Association and click on the ElectionStatus entity, hold the mouse button down and it will allow you to draw a line to the Election entity.

绘制线条后,它将询问您关联中涉及哪些属性.您要从Election实体中选择StatusId列,并从ElectionStatus实体中选择StatusId列.

Once you've drawn the line it will ask you which properties are involved in the association. You want to select the StatusId column from the Election entity, and the StatusId column from the ElectionStatus entity.

现在,您已经完成了映射,您将可以大大简化查询,因为不需要联接.您只需通过LINQ to SQL将添加到选举实体的全新属性访问选举状态.

Now that you've completed your mapping you will be able to simplify your query greatly because the join will not be necessary. You can just access the election status via a brand new property that LINQ to SQL will have added to the Election entity.

您的代码现在可以如下所示:

Your code can now look like this:

//context has to be moved outside the function
static ExampleDataContext context = new ExampleDataContext();

//Here we can return an IEnumerable of Election now, instead of using the Tuple class
public static IEnumerable<Election> getElections()
{
    return from election in context.Elections
           select election;
}

static void Main(string[] args)
{
    //get the elections
    var elections = getElections();

    //lets go through the elections
    foreach (var election in elections)
    {
        //here we can access election status via the ElectionStatus property
        Console.WriteLine("Election name: {0}; Election status: {1}", election.ElectionName, election.ElectionStatus.StatusDescription);
    }
}

您还可以在LINQ to SQL关联上找到操作方法" 这里.

You can also find a "how to" on LINQ to SQL associations here.

注意:值得一提的是,如果您在数据库中的表之间建立了FK关系; LINQ to SQL将自动选择该关系并为您映射关联(因此创建属性).

Note: It's worth mentioning that if you have an FK relationship set up between your tables in the database; LINQ to SQL will automatically pick the relationship up and map the association for you (therefore creating the properties).

这篇关于如何从使用LINQ到SQL的方法返回查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆