将列表传递给 Linq 查询

发布于 2024-10-29 09:09:17 字数 765 浏览 1 评论 0原文

我有一个返回列表的 Linq 查询,结果如下所示:

protected void Page_Load(object sender, EventArgs e)
{
  var MyList = GetPatientsFromDB(TheUserID);
}

此列表的类型为 MyModel,如下所示:

MyModel
{
public int PatientID {get;set;}
}

现在我要做的就是将此列表传递给名为 GetPatientInfo 的函数,并返回另一个 MyOtherModel

MyOtherModel{
public int PatientID {get;set;}
public string Name {get;set;}
public string Region {get;set;}
}

I' 列表我在编写第二个函数时遇到一些问题。

我一开始

public static List<MyOtherModel> GetPatientInfo(List<MyModel>
{

using (..... MyDC = new... DataContext)

{
     var OutputList = from f in MyDC.Table
                       where......?

}

就坚持编写 where 子句和调用语句。我怎样才能做到这一点?

I have a Linq query that returns a list and the result looks like this:

protected void Page_Load(object sender, EventArgs e)
{
  var MyList = GetPatientsFromDB(TheUserID);
}

This list is of type MyModel like this:

MyModel
{
public int PatientID {get;set;}
}

Now what I'm looking to do is pass this list to a function called GetPatientInfo and return another list of MyOtherModel

MyOtherModel{
public int PatientID {get;set;}
public string Name {get;set;}
public string Region {get;set;}
}

I'm have some problems writing the second function.

I started with

public static List<MyOtherModel> GetPatientInfo(List<MyModel>
{

using (..... MyDC = new... DataContext)

{
     var OutputList = from f in MyDC.Table
                       where......?

}

I'm stuck on writing the where clause and the calling statement. How can I do that?

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

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

发布评论

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

评论(3

伪装你 2024-11-05 09:09:17
public static List<MyOtherModel> GetPatientInfo(List<MyModel list>
{
    using (..... MyDC = new... DataContext)
    {
        var result = from f in MyDC.Table
                     where list.Select(m => m.PatientID).Contains(f.PatientID)
                     select f;

        return result.ToList();
    }
}
public static List<MyOtherModel> GetPatientInfo(List<MyModel list>
{
    using (..... MyDC = new... DataContext)
    {
        var result = from f in MyDC.Table
                     where list.Select(m => m.PatientID).Contains(f.PatientID)
                     select f;

        return result.ToList();
    }
}
屌丝范 2024-11-05 09:09:17

为了使其完全保持在查询语法中,它会是这样的:

var OutputList = from f in MyDC.Table
    from m in list
    where f.PatientId == m.PatientId
    select f;

但是,这是否真正有效取决于您使用的 LINQ 提供程序。这是 LINQ To SQL 吗?物体?实体?根据这是哪个提供者,它可能没有可以支持此查询的幕后实现。如果是这种情况,您可能被迫在 MyDC.Table (MyDC.Table.AsEnumerable()) 上添加 AsEnumerable(),或者完全重新考虑您的查询。 AsEnumerable 会将整个表放入内存,然后从那时起使用 LINQ to Objects,这可能是一个昂贵的举动。

To keep it fully in query syntax, it would be something like this:

var OutputList = from f in MyDC.Table
    from m in list
    where f.PatientId == m.PatientId
    select f;

However, whether this actually works or not depends on what LINQ provider you are using. Is this LINQ To SQL? Objects? Entities? Depending on which provider this is, it may not have an implementation behind the scenes that can support this query. If that is the case, you may be forced to either throw in AsEnumerable() on MyDC.Table (MyDC.Table.AsEnumerable()), or rethink your query altogether. AsEnumerable will bring the entire table into memory and then use LINQ to Objects from that point on, potentially an expensive move.

哀由 2024-11-05 09:09:17
public static List<MyOtherModel> GetPatientInfo(List<MyModel> patients)
{

    using (..... MyDC = new... DataContext)
    {
        var OutputList = from f in MyDC.Table
                         where patients.Any(p => p.PatientID == f.PatientID)
                         select f;
    }
}
public static List<MyOtherModel> GetPatientInfo(List<MyModel> patients)
{

    using (..... MyDC = new... DataContext)
    {
        var OutputList = from f in MyDC.Table
                         where patients.Any(p => p.PatientID == f.PatientID)
                         select f;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文