I am trying to search DB using LINQ to SQL and display it. 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
There may be Multiple Results having Same ID so Use 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));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.