简体   繁体   English

将来自不同存储过程的结果合并到c#中的单个列表中

[英]Combine result from different stored procedure into a single list in c#

I am trying to join two different list together using the Concat Method as shown below. 我正在尝试使用Concat方法将两个不同的列表连接在一起,如下所示。 I need the second list to be added to the first list, so that it can come as one lIst. 我需要将第二个列表添加到第一个列表中,以便它能作为一个整体出现。

var term = dc.AgentTerm(Agentcode).ToList();
var hosp = dc.AgentHospCashPlan(Agentcode).ToList();
var life = term.Concat(hosp).ToList();

But when i tried it, I get this error. 但是,当我尝试它时,出现此错误。

Error 16 Instance argument: cannot convert from System.Collections.Generic.List<IbsMobile.AgentTerm_Result> to System.Linq.IQueryable<IbsMobile.AgentHospCashPlan_Result> C:\\Project\\IbsMobile\\IbsMobile\\Controllers\\ParamController.cs 506 24 IbsMobile 错误16实例参数:无法从System.Collections.Generic.List<IbsMobile.AgentTerm_Result>转换为System.Linq.IQueryable<IbsMobile.AgentHospCashPlan_Result> C:\\ Project \\ IbsMobile \\ IbsMobile \\ Controllers \\ ParamController.cs 506 24 IbsMobile

Error 17 System.Collections.Generic.List<IbsMobile.AgentTerm_Result> does not contain a definition for 'Concat' and the best extension method overload System.Linq.Queryable.Concat<TSource>(System.Linq.IQueryable<TSource>, System.Collections.Generic.IEnumerable<TSource>) has some invalid arguments C:\\Project\\IbsMobile\\IbsMobile\\Controllers\\ParamController.cs 506 24 IbsMobile 错误17 System.Collections.Generic.List<IbsMobile.AgentTerm_Result>不包含'Concat'的定义,并且最佳扩展方法重载System.Linq.Queryable.Concat<TSource>(System.Linq.IQueryable<TSource>, System.Collections.Generic.IEnumerable<TSource>)有一些无效的参数C:\\ Project \\ IbsMobile \\ IbsMobile \\ Controllers \\ ParamController.cs 506 24 IbsMobile

I checked online and all I see is how to convert 'System.Linq.IQueryable' to 'System.Collections.Generic.IList'. 我在线检查了一下,所看到的是如何将“ System.Linq.IQueryable”转换为“ System.Collections.Generic.IList”。

The results that I'm assigning to term and Hosp are from stored procedures. 我分配给term和Hosp的结果来自存储过程。

I have used the concat method before on lists with the same class and it worked fine. 我之前在具有相同类的列表上使用过concat方法,并且效果很好。

I am guessing this error is because the results sets are from different classes. 我猜这个错误是因为结果集来自不同的类。

I know this is a novice question but I'm not sure exactly what I'm doing wrong. 我知道这是一个新手问题,但我不确定我在做什么错。

You cannot Concat lists of two different source types. 您不能Concat两个不同源类型的列表。 See functions signature: 查看函数签名:

public static System.Collections.Generic.IEnumerable<TSource> Concat<TSource> 
    (this System.Collections.Generic.IEnumerable<TSource> first, 
     System.Collections.Generic.IEnumerable<TSource> second);

To still do so project each list into some type that share the same base type or interface. 仍要这样做,将每个列表投影到共享相同基本类型或接口的某种类型。 Something of the structure: 结构的东西:

public interface IInterface {}
public class AgentTermDTO : IInterface {}
public class AgentHospCashPlanDTO : IInterface {}

or a quick and dirty solution (don't go for this one) is to cast each to a collection of object s: list1.Cast<object>().Concate(...) 或快速而又肮脏的解决方案(不要这样做)是将每个object list1.Cast<object>().Concate(...)object s的集合: list1.Cast<object>().Concate(...)

If you would have written the full definition of term and hosp instead of var, you would have written something like: 如果您将编写term和hosp的完整定义而不是var,则可能会编写如下内容:

List<AgentTerm_Result> terms = ...
List<AgentHospCashPlan_Result> hosps = ...

Apparently they are not of the same type. 显然,它们不是同一类型。 You can only concatenate sequences of similar type. 您只能串联相似类型的序列。 Alas you forgot to write us your classes. las,您忘了给我们写课。 If a term and a hosp are related, for instance if they have the same super class, you could cast them to the super class before contatenating: 如果一个术语和一个医院相关,例如,如果它们具有相同的超类,则可以在污染之前将它们转换为超类:

var result = terms.Cast<CommonSuperClass>()
    .Concat(hosps.Cast<CommonSuperClass>();

This also works if they implement the same interface. 如果他们实现相同的接口,这也将起作用。

If there is nothing common between them, then maybe you should reconsider the idea about concatenating these two lists. 如果它们之间没有共同之处,那么也许您应该重新考虑有关串联这两个列表的想法。 After all, what is the use of the collection of Cows mixed with a collection of NewYorkCityBuildings ? 毕竟,将Cows的集合与NewYorkCityBuildings的集合混合使用有什么用?

Anyway, if you really think there is something common, there are several solutions to concatenate the sequences. 无论如何,如果您真的认为有一些共同点,那么可以使用多种解决方案来连接序列。 Of course you can only use the properties they have in common. 当然,您只能使用它们共有的属性。

Consider creating an interface that contains the properties that you want to access: 考虑创建一个包含要访问的属性的接口:

interface IMyInterface
{
     // Properties that are both in AgentTerm_Result and in AgentHospCashPlan
     int Id {get;}
     string Name {get;}
}

class AgentTerm_Result : IMyInterface {...}
class AgentHospCashPlan : IMyInterface{...}

var result = terms.Cast<IMyInterface>()
     .Concat(hosps.Cast<IMyInterface>();

If there are no common properties, consider creating a wrapper class that converts the properties to the proper ones: 如果没有通用属性,请考虑创建一个包装器类,将这些属性转换为适当的属性:

class AgentTerm : IMyInterface
{
     public AgentTerm_Result TermResult {get; set;}

     public int Id => this.TermResult.TermResultId;
     public string Name => this.TermResult.TermResultName;

} }

and similar for Hosp 和类似的医院

IEnumerable<IMyInterface> myTerms = terms.Select(term => new AgentTerm
    {
         TermResult = term,
    })
    .Cast<IMyInterface>();
IEnumerable<IMyInterface> myHosps = hosps.Select(hosp => new MyHosp
    {
        Hosp = hosp,
    })
    .Cast<IMyInterface>();

var result = myTerms.Concat(myHosps);

If you want to return two lists containing two different types, the best option is to return a custom type or even just return a tuple. 如果要返回两个包含两种不同类型的列表,最好的选择是返回自定义类型,甚至只返回一个元组。

To return a custom type, just define up a class to hold the return values: 要返回自定义类型,只需定义一个类来保存返回值:

public class Results
{
    public List<AgentTerm_Result> AgentTerms;
    public List<AgentHospCashPlan_Result> AgentHospCashPlan;
}

Now you can easily do this: 现在您可以轻松地做到这一点:

public Results GetResults()
{
    var term = dc.AgentTerm(Agentcode).ToList();
    var hosp = dc.AgentHospCashPlan(Agentcode).ToList();

    return new Results { AgentTerms = term, AgentHospCashPlan = hosp };
}

To use a tuple is slightly easier as you don't need to define anything new: 使用元组稍微容易些,因为您不需要定义任何新内容:

public (List<AgentTerm_Result>, List<AgentHospCashPlan_Result>) GetResults()
{
    var Agentcode = "";

    var term = dc.AgentTerm(Agentcode).ToList();
    var hosp = dc.AgentHospCashPlan(Agentcode).ToList();

    return (term, hosp);
}   

To get the results of the tuple just call it like this: 要获取元组的结果,只需像这样调用它:

var (term, hop) = GetResults();

Then you can use the results as two separate variables. 然后,您可以将结果用作两个单独的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM