gpt4 book ai didi

c# - Linq to Sql-过滤表结果

转载 作者:太空宇宙 更新时间:2023-11-03 18:03:58 24 4
gpt4 key购买 nike

我有一个SQL表:

Id  FirstName   LastName    DateTime
1 John Doe 2016-09-27 20:45:52.293
2 John Doe 2016-09-27 20:45:53.620
3 John Doe 2016-09-27 20:46:02.370
4 John Doe 2016-09-27 20:46:02.533
5 John Doe 2016-09-27 20:46:02.680
6 John Doe 2016-09-27 20:46:02.820


还有一个类和实例:

public class Cus
{
public int Id { get; set; }

public string First { get; set; }

public string Last { get; set; }

public DateTime DateTime { get; set; }
}

List<Cus> o = new List<Cus>()
{
new Cus()
{
Id = 1,
First = "John",
Last = "Doe"
},
new Cus()
{
Id = 2,
First = "John",
Last = "Doe"
},
new Cus()
{
Id = 3,
First = "John",
Last = "Doe"
}
};


我使用Linq To Sql类获取我的sql表中的所有记录。我做 :

using(DataClassesDataContext Context = new DataClassesDataContext())
{
var Customers = Context.Customers.Where(x => x.Any(y => o. ????????;
}


但我只想将 List<Cus>中的客户(使用 Id)放在我的表中

这该怎么做 ?

最佳答案

您可以从o集合中获取所有ID,并使用Contains方法过滤掉我们创建的用户ID集合中的ID的客户。

//Get the Id's from o collection.
var userIds = o.Select(f=>f.Id);

//Get the customers who's id belongs to the above collection
var customers = yourDbContext.Customers.Where(x => userIds.Contains(x.Id)).ToList();

关于c# - Linq to Sql-过滤表结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39732940/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com