LINQ to SQL 将查询结果存储在变量中

发布于 2025-01-08 11:38:43 字数 197 浏览 1 评论 0原文

例如,我正在搜索特定人员 ID,并且希望将该 ID 存储到局部变量或实例变量中。如何使用 LINQ to SQL 检索查询结果并将其存储在 int 变量中? 假设我们有这个查询

from user in dbo.DoctorsName
where doctorsName  = "Foo Bar"
select DOC_ID;

For example I am searching for a specific persons ID and I want to store that ID to a local variable or instance variable. How do I retrieve Query results and stores them in a int Variable with LINQ to SQL?
Assuming we have this query

from user in dbo.DoctorsName
where doctorsName  = "Foo Bar"
select DOC_ID;

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

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

发布评论

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

评论(3

镜花水月 2025-01-15 11:38:43

您可以像这样使用 FirstOrDefault() :

var results = from user in dbo.DoctorsName
              where user.doctorsName  == "Foo Bar"
              select user;

string personName = results.FirstOrDefault().Name;

You can use FirstOrDefault() like this:

var results = from user in dbo.DoctorsName
              where user.doctorsName  == "Foo Bar"
              select user;

string personName = results.FirstOrDefault().Name;
聽兲甴掵 2025-01-15 11:38:43

这应该有帮助。

var name = dbo.DoctorsName.Where(item => item.doctorsName = "Foo Bar").Select(item => item.Name).FirstOrDefault();

如果没有符合您的条件的记录,则使用 FirstOrDefault() 可能会引发异常。为此,您可以尝试使用 -

var namelist = dbo.DoctorsName.Where(item => item.doctorsName = "Foo Bar").Select(item => item.Name);

If(namelist.Count() > 0)
   var name = namelist.Fisrt();

This should help.

var name = dbo.DoctorsName.Where(item => item.doctorsName = "Foo Bar").Select(item => item.Name).FirstOrDefault();

If there are no records for your condition then using FirstOrDefault() can throw an exception. For this you can try using -

var namelist = dbo.DoctorsName.Where(item => item.doctorsName = "Foo Bar").Select(item => item.Name);

If(namelist.Count() > 0)
   var name = namelist.Fisrt();
东北女汉子 2025-01-15 11:38:43

与此类似:

var ticketDepartment = from t in dt_tickets.AsEnumerable()
                       where t.Field<string>("MID") == mid
                       select new { department = t.Field<string>("Department") };

现在您的变量位于 TicketDepartment.department 中

Similar to this:

var ticketDepartment = from t in dt_tickets.AsEnumerable()
                       where t.Field<string>("MID") == mid
                       select new { department = t.Field<string>("Department") };

and now you have your variable in ticketDepartment.department

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文