Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从使用LINQ到SQL的方法返回查询结果_C#_Linq_Linq To Sql - Fatal编程技术网

C# 如何从使用LINQ到SQL的方法返回查询结果

C# 如何从使用LINQ到SQL的方法返回查询结果,c#,linq,linq-to-sql,C#,Linq,Linq To Sql,这是我正在使用的代码,我对LINQ还是有点陌生,所以这是一项正在进行的工作。具体地说,我希望从这个查询中获得结果(大约7列字符串、int和datetime),并将它们返回给调用包含这个LINQ-to-SQL查询的方法的方法。一个简单的代码示例将非常有用 using (ormDataContext context = new ormDataContext(connStr)) { var electionInfo = from t1 in context.elections

这是我正在使用的代码,我对LINQ还是有点陌生,所以这是一项正在进行的工作。具体地说,我希望从这个查询中获得结果(大约7列字符串、int和datetime),并将它们返回给调用包含这个LINQ-to-SQL查询的方法的方法。一个简单的代码示例将非常有用

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

(在本例中,我的查询返回两个表的所有内容,election和election_status。)

您需要创建与匿名类型具有相同结构的类。然后,使用“newmyclass(t1,t2)”,而不是“new{t1,t2}


一旦你有了一个命名类,你就可以像你希望的那样把它到处传递。

你需要创建与匿名类型具有相同结构的类。然后,使用“newmyclass(t1,t2)”,而不是“new{t1,t2}


一旦你有了一个命名类,你就可以像你希望的那样把它到处传递。

你不能从一个方法返回一个匿名类型。它仅在创建它的范围内可用。您必须创建一个类。

您不能从方法返回匿名类型。它仅在创建它的范围内可用。您将不得不创建一个类

具体来说,我想得到我的 此查询的结果(约7个) 字符串、int和 datetime),并返回它们

您好,您的查询遇到的问题是您正在创建一个匿名类型。您无法从方法返回匿名类型,因此这将是您遇到问题的地方

您需要做的是创建一个“包装器”类型,它可以接受选举和选举状态,然后返回这些状态

这是我所说的一个小例子;如您所见,我声明了一个元组类。将封装查询的方法返回IEnumerable

我希望这有帮助:-)

类元组
{
选举;
选举状态选举状态;
公共元组(选举,选举状态选举状态)
{
这个选举=选举;
this.election\u status=election\u status;
}
}
公共IEnumerable getElections()
{
IEnumerable结果=null;
使用(ormDataContext=new或mdatacontext(connStr))
{
结果=来自context.elections中的t1
在context.election\u状态中加入t2
在t1上,statusID等于t2.statusID
选择新的元组(t1,t2);
}
}
更新

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

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

  • 单击Association并单击ElectionStatus实体,按住鼠标按钮,它将允许您绘制一条到选举实体的线

  • 一旦你划清界限,它会问你哪些属性参与了关联。您要从选举实体中选择StatusId列,从ElectionStatus实体中选择StatusId列

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

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

//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);
    }
}
//必须将上下文移到函数外部
静态ExampleDataContext上下文=新ExampleDataContext();
//现在我们可以返回IEnumerable of Election,而不是使用Tuple类
公共静态IEnumerable getElections()
{
从选举中返回。选举
选择选举;
}
静态void Main(字符串[]参数)
{
//赢得选举
var elections=getElections();
//让我们进行选举吧
foreach(选举中的var选举)
{
//在这里,我们可以通过ElectionStatus属性访问选举状态
Console.WriteLine(“选举名称:{0};选举状态:{1}”,Election.ElectionName,Election.ElectionStatus.StatusDescription);
}
}
您还可以找到关于LINQ到SQL关联的“操作方法”

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

具体来说,我想得到我的 此查询的结果(约7个) 字符串、int和 datetime),并返回它们

您好,您的查询遇到的问题是您正在创建一个匿名类型。您无法从方法返回匿名类型,因此这将是您遇到问题的地方

您需要做的是创建一个“包装器”类型,它可以接受选举和选举状态,然后返回这些状态

这是我所说的一个小例子;如您所见,我声明了一个元组类。将封装查询的方法返回IEnumerable

我希望这有帮助:-)

类元组
{
选举;
选举状态选举状态;
公共元组(选举,选举状态选举状态)
{
这个选举=选举;
this.election\u status=election\u status;
}
}
公共IEnumerable getElections()
{
IEnumerable结果=null;
使用(ormDataContext=new或mdatacontext(connStr))
{
结果=来自context.elections中的t1
在context.election\u状态中加入t2
在t1上,statusID等于t2.statusID
选择新的元组(t1,t2);
}
}
更新

根据NagaMensch的评论,一个更好的实现achi的方法
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);
    }
}
//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);
    }
}
public IList<election> GetElections()
{
    using (ormDataContext context = new ormDataContext(connStr))
    {
        DataLoadOptions dlo = new DataLoadOptions();
        dlo.LoadWith<election>(e => e.election_status);
        context.DeferredLoadingEnabled = false;
        context.LoadOptions = dlo;   
        return context.elections.ToList();
    }
}
IList<election> elections = GetElections();

// Access the election status.
Console.WriteLin(elections[0].election_status);
ormDataContext context = new ormDataContext(connStr));

IList<election> elections = context.elections.ToList();

// This will trigger a query that loads the election
// status of the first election object.
Console.WriteLine(elections[0].election_status);
IEnumerable<object> getRecordsList()
    {
        using (var dbObj = new OrderEntryDbEntities())
        {
            return (from orderRec in dbObj.structSTIOrderUpdateTbls
                    select orderRec).ToList<object>();
        }
    }
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.column1,
                    t1.column2,
                    t1.column3,
                    t1.column4,
                    t2.column5,
                    t2.column6,
                    t2.column7
                    };
}
foreach (var ei in electionInfo){
//write what do u want
}