First time using LinqtoSQL. So to give you guys a bit of context:
I have a simple SQL Server table called Inventory with the following fields
InventoryID (int)(autoincrementing)
InventoryItemName (varchar)(255)
InventoryCategory (int)
Next I have another table called InventoryCategories for with the following fields:
InventoryCategoryID (int)(autoincrementing)
InventoryCategoryName (varchar)(255)
InventoryCategoryDescription (varchar)(255)
Next, Currently I have a combo box which selects which query to update the DataGrid.ItemSource, The code fo for this is below
if (searchcategory == "All Stock")
{
InventoryDataContext dc = new InventoryDataContext();
var q =
from a in dc.GetTable<Inventory>()
select a;
SearchResults.ItemsSource = q;
}
Now this result returns the Full table of Inventory with the columns of InventoryID, InventoryItemName, and InventoryCategory. However it returns the ID Number of InventoryCategory, in the InventoryCategory Column.
Would anyone be able to help me get the InventoryCategoryName from InventoryCategories Table in this query instead of the ID? What would be required for this?
try using left join:
var qry = from inv in context.Inventory
from invCategory in context.InventoryCategories.Where(u => inv.InventoryCategory == u.InventoryCategoryID).DefaultIfEmpty()
select new myViewModel {id = invCategory.InventoryCategoryID, categoryName = invCategory .InventoryCategoryName }
and don't forget to create myViewModel class with id and categoryName properties
Related
I have an ID list of 75000 records.Based on this I need to fetch corresponding items from another table which have 1700000+ records in c#. I tried with foreach and it taking 1+ hours.Both tables are in different servers. I tried contains in linq and it's not working. Please suggest me some better options.
foreach(var item in cotext1.Table1)
{
var Employee=Context2.table2.where(x=>x.EmpId==item.Id);
}
I tried to join, but getting out of memory exception.
To clarify what was discussed in comments:
Create user defined type which corresponds to your list with IDs (since it doesn't make sense for ids to repeat in your list - I made it primary key):
CREATE TYPE dbo.IntHashSet AS TABLE
(
ID int NOT NULL PRIMARY KEY
)
Query:
using (var ctx = new TestDBEntities()) {
// fill data table with your ids
var dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
for (int i = 0; i < 75000; i++) {
dt.Rows.Add(i);
}
// make a query
var result = ctx.Database.SqlQuery<BigTable>("select BT.* from BigTable BT inner join #IDS I on BT.CodeID = I.ID",
new SqlParameter("IDS", SqlDbType.Structured)
{
// name of type you created in step 1
TypeName = "dbo.IntHashSet",
Value = dt
}).ToArray();
}
This query with 75.000 ids against table with 1.700.000 rows takes about 600ms on my (non-server, consumer grade) machine. Which is several orders of magnituge faster than what it takes now with your query (more than one hour).
You can use a join statement in linq
Server1Entities dc1 = new Server1Entities ();
List<IDTable> ids = (from ro in dc1.IDTable select ro).ToList();
Server2Entities dc2 = new Server2Entities();
var list = (from firstTable in ids
join secondTable in dc.YourSecondTable
on firstTable.ID equals secondTable.ID
select new {
field1 = firstTable.Field1,
field2 = secondTableField1 // You can define fieldnames as you want.
}).ToList();
Another option is using the morelinq library. Really extraordinary.
you can take a look :
https://github.com/morelinq/MoreLINQ
I am new to LINQ to SQL and I am getting a weird result with my testing program that I don't want.
The program is a very simple WinForms app that shows SQL results in a DataGridView.
Here is my database setup.
Here is the specific column information per table.
And finally here are the results of the table shown through MSMS
Here is the issue that i'm having.
After establishing the connection to the database, I then run the following.
testingDatabase = new DataClasses1DataContext(SharedVariables.TestingConnection);
var query = from Orders in testingDatabase.GetTable<ORDER>()
select Orders;
//select new { Orders.CUST_ID, Orders.ORDER_NUM, Orders.ORDER_DATE };
return query;
It returns this.
I have also tried this code
testingDatabase = new DataClasses1DataContext(SharedVariables.TestingConnection);
var query = from Orders in testingDatabase.ORDERs
select Orders;
//select new { Orders.CUST_ID, Orders.ORDER_NUM, Orders.ORDER_DATE };
return query;
However the results are the same.
Then only way I can get the results i'm looking for is by doing this
testingDatabase = new DataClasses1DataContext(SharedVariables.TestingConnection);
var query = from Orders in testingDatabase.GetTable<ORDER>()
//select Orders;
select new { Orders.CUST_ID, Orders.ORDER_NUM, Orders.ORDER_DATE };
return query;
Then I finally get this.
Can anyone tell me how to do a "select customers" and have it not show the column displaying "LINQ_Testing.CUSTOMER". I suspect it's because it's the foreign key causing that since it makes an EntityRef variable in the CUSTOMER class.
Try adding a virtual object of Customer class in Order class:
public virtual Customer customer { get; set; }
The foreign key relation in Order class (i.e cust_id) will lazy load the Customer object in it automatically.
I using C# and LINQ to pull/push data housed in SQL Azure. The basic scenario is we have a Customer table that contains all customers (PK = CompanyID) and supporting tables like LaborTypes and Materials (FK CompanyID to Customer table).
When a new customer signs up, a new record is created in the Customers table. Once that is complete, I want to load a set of default materials and laborTypes from a separate table. It is simple enough if I just wanted to copy data direct from one table to another but in order to populate the existing tables for the new customer, I need to take the seed data (e.g. laborType, laborDescription), add the CompanyID for each row of seed data, then do the insert to the existing table.
What the best method to accomplish this using C# and LINQ with SQL Azure?
An example of a direct insert from user input for LaborTypes is below for contextual reference.
using (var context = GetContext(memCustomer))
{
var u = GetUserByUsername(context, memUser);
var l = (from lbr in context.LaborTypes
where lbr.LaborType1.ToLower() == laborType
&& lbr.Company == u.Company
select lbr).FirstOrDefault();
if (l == null)
{
l = new AccountDB.LaborType();
l.Company = u.Company;
l.Description = laborDescription;
l.LaborType1 = laborType;
l.FlatRate = flatRate;
l.HourlyRate = hourlyRate;
context.LaborTypes.InsertOnSubmit(l);
context.SubmitChanges();
}
result = true;
}
What you'll want to do is write a query retrieving data from table B and do an Insert Statement on Table A using the result(s).
This has been covered elsewhere in SO I think, here would be a good place to start
I don't know the syntax for Linq specifically; but by constructing something similar to #Murph 's answer beyond that link, I think this might work.
var fromB= from b in TableB
where ... ;//identify the row/data from table B
// you may need to make fromB populate from the DB here.
var toInsert = ...; //construct your object with the desired data
// do other necessary things here
TableA.InsertAllOnSubmit(toInsert);
dc.SubmitChanges(); // Submit your changes
I would like to bind a dropdownlist with a product name and an orderItemID as its value (both from two separate tables but with relations)
Heres the database design (some columns left out for brevity)
Order - ID, Name, address
OrderItems - ID, OrderID, ProductID, Price
Products - ID, Name, description, Price
Heres the code to connect it all
var oList = ReturnOrderItems(OID).Select(pr => new {pr.Product.Name }).ToList();
DropdownListItems.DataSource = oList;
DropdownListItems.DataTextField = "NEED THE ORDER ITEM ID";
DropdownListItems.DataValueField = "Name";
DropdownListItems.DataBind();
ReturnOrderItems is a simple method to get all order items by order ID.
Then i added a Select to return the product name (as OrderItems has a relationship to the products table).
So far this works and returns the product name which is fine but how could i get the ID of the order item (which would be held inside the ReturnOrderItems(OID) method?
I've tried several ways but the most i get is productID which is no good - how should i be doing this?
Assuming that the method returns a collection ofOrderItem then you would do
var oList = ReturnOrderItems(OID).Select(pr => new {pr.Product.Name, pr.ID }).ToList();
Also are you sure you want to store the name and display the id, otherwise i think you want this:
DropdownListItems.DataTextField = "Name";
DropdownListItems.DataValueField = "ID";
I have two tables I am using to fill a gridview. The tables have a common field named RangeActivityID. My problem is that the database is very old and some of the older entries do not match up IDs between tables, so I am unable to add an association between them in the database.
I do not care about the old data which doesn't match up, so in my .dbml file, I manually created an association in order to select good data from both tables. This is the LINQ query I have:
var collective = from p in rangeConnection.RangeActivities
orderby p.RangeActivityID
select new
{
TestName = p.TestName,
DateTime = p.ActivityDateTime,
Notes = p.Notes,
//RoundSerialNumber = p.RoundFire.RoundSerialNumber,
//RoundType = p.RoundFire.RoundType,
//LotStockNumber = p.RoundFire.LotNumber
};
I can set my grid datasource to 'collective' and everything works, but if I uncomment the three commented lines, the query returns no results because the tables have data that doesn't meet the association criteria. Is there a way to have the LINQ query ignore results that do not match up?
Thanks in advance!
Try to add where p.RoundFire != null criteria.
Suggest a join instead, and emulating a SQL LEFT JOIN.
var q = from p in rangeConnection.RangeActivities
join r in rangeConnection.RoundFires
on p.RangeActivityID equals r.RangeActivityID into sr
from x in sr.DefaultIfEmpty()
select new
{
TestName = p.TestName,
DateTime = p.ActivityDateTime,
Notes = p.Notes,
RoundSerialNumber = x.RoundSerialNumber,
RoundType = x.RoundType,
LotStockNumber = x.LotNumber
//consider checking for string.IsNullOrEmpty()
//for the RoundFires properties
};
The syntax for your entities may be inaccurate, but please edit my answer if it helps lead you to a solution.