[英]Search SQL db using LINQ to SQL
I am trying to search DB using LINQ to SQL and display it. 我正在尝试使用LINQ to SQL搜索数据库并显示它。 I tried below code but it not showing record matching to criteria on console.
我尝试了以下代码,但未在控制台上显示与条件匹配的记录。
Here Details is table name 这里的细节是表名
Detail detail = new Detail();
Console.Write("\nEnter ID to Search Record :");
int id = Convert.ToInt32(Console.ReadLine());
var searchbyId = from search in db.Details
where search.Id == id
select search;
db.SubmitChanges();
Console.WriteLine("\n Search Results \n");
Console.WriteLine(String.Format("Id \t | Name \t | Last Name \n"));
Console.WriteLine(String.Format("{0} \t | {1} \t | {2} ",
detail.Id,detail.Name, detail.LastName));
Newbie trying to learn LINQ 新手尝试学习LINQ
There may be Multiple Results having Same ID so Use foreach
可能有多个具有相同ID的结果,因此请使用
foreach
Console.Write("\nEnter ID to Search Record :");
int id = Convert.ToInt32(Console.ReadLine());
var searchbyId = from search in db.Details
where search.Id == id
select search;
Console.WriteLine("\n Search Results \n");
Console.WriteLine(String.Format("Id \t | Name \t | Last Name \n"));
foreach(var item in searchbyId )
Console.WriteLine(String.Format("{0} \t | {1} \t | {2} ",
item.Id,detail.Name, item.LastName));
I would try something like that: 我会尝试这样的事情:
Console.Write("\nEnter First Name :");
string id = Convert.ToInt32(Console.ReadLine());
var searchbyId = from search in db.Details
where search.Id == id
select search;
Console.WriteLine("\n Search Results \n");
Console.WriteLine(String.Format("Id \t | Name \t | Last Name \n"));
foreach(var item in searchbyId )
Console.WriteLine(String.Format("{0} \t | {1} \t | {2} ",
searchbyId.Id,detail.Name, searchbyId.LastName));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.