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

这是我正在使用的代码,我对LINQ仍然有点新,所以这是一项正在进行中的工作。 具体来说,我想从此查询中获取结果(大约7列字符串,整数和日期时间),并将它们返回给调用包含此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 }; } 

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

具体来说,我想从此查询中获取结果(大约7列字符串,整数和日期时间),并返回它们

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

你需要做的是创建一个“包装”类型,可以进行选举和选举 – 状态,然后返回那些。

这是我正在谈论的一些小样本; 正如你所看到的,我宣布了一个Tuple类。 您将查询包装的方法返回IEnumerable。

我希望这有帮助 :-)

 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 getElections() { IEnumerable 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); } } 

UPDATE

根据NagaMensch的评论,实现所需结果的更好方法是使用内置的LINQ to SQL关联。

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

  • 单击关联并单击ElectionStatus实体,按住鼠标按钮,它将允许您向选举实体绘制一条线。

  • 绘制线后,它会询问您关联中涉及哪些属性。 您希望从Election实体中选择StatusId列,并从ElectionStatus实体中选择StatusId列。

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

您的代码现在看起来像这样:

 //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 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关联的“操作方法”。

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

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

一旦你有一个命名的类,你可以像你希望的那样将它传递到所有地方。

问题是,您正在创建一个匿名类型,因此无法使用此返回类型声明方法。 您必须创建一个新类型来保存您的查询结果并返回此类型。

但我建议不要以新类型返回结果,而只返回election对象的集合,并通过关系属性访问election_status对象,假设您将它们包含在模型中。 数据加载选项使查询在查询结果中包含相关的选举状态对象。

 public IList GetElections() { using (ormDataContext context = new ormDataContext(connStr)) { DataLoadOptions dlo = new DataLoadOptions(); dlo.LoadWith(e => e.election_status); context.DeferredLoadingEnabled = false; context.LoadOptions = dlo; return context.elections.ToList(); } } 

现在您可以执行以下操作。

 IList elections = GetElections(); // Access the election status. Console.WriteLin(elections[0].election_status); 

我通常LINQ to SQL可以根据需要检索相关实体 – 这称为延迟加载。

 ormDataContext context = new ormDataContext(connStr)); IList 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); 

但是,这要求您在完成使用检索到的对象之前不要关闭数据上下文,因此不能与封装在方法中的using语句一起using

 IEnumerable getRecordsList() { using (var dbObj = new OrderEntryDbEntities()) { return (from orderRec in dbObj.structSTIOrderUpdateTbls select orderRec).ToList(); } } 

也许它可以帮助:)

 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 } 
 return electionInfo.ToList(); 

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