将列表传递给 Linq 查询
我有一个返回列表的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了使其完全保持在查询语法中,它会是这样的:
但是,这是否真正有效取决于您使用的 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:
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.