I am trying use LINQ in a C# program to get information from a database. I have found a lot of examples showing basic to advanced queries, but I can’t find anything to get a basic database connection. See basic LINQ example below:
var adultNames = from person in people
where person.Age >= 18
select person.Name;
I just can’t figure out how to get to a specific database table and return information out of it. I want to be able to connect to a db table and return some information out of it. Thanks!
Theres a very simple way to connect to a database although I suggest you read the app config afterwards and other parts of your project to see how it works.
In the menu strip go to Data -> Add New Data Source. From here pick database then entity data model. Follow the rest of the steps to create a datasource for your project.
In your program you can create an object of the datasource by using:
WhatYouCalledTheDataSource datasourceObject = new WhatYouCalledTheDataSource();
You can then make queries on this object by changing your first line of the query to:
to get the data you want, once you have the query you can place the results into a list like:
bearing in mind that I basing on the thought that name would be a string.
You can also create lists based on tables like