Hi,
suppose you have two SQL tables that represent Users and related Location:
CREATE TABLE Users (
ID int IDENTITY (1,1)
FirstName varchar(255)
LastName varchar(255)
ID_LOCATION int
)
CREATE TABLE Locations(
Name varchar (255)
Suppose that there is a FK relation between Locations.ID and Users.ID_LOCATION.
Then I would like to create the following object:
MyDataContext dc = new MyDataContext()
var USER_LOCATION = from u IN dc.Users
select new
{
u.ID,
u.FirstName,
u.LastName,
LocationName = u.Locations.Name
}
and use it as datasource for the Wingrid.
My problem is that when I try to do a SubmitChanges operation on the DataContext, I got no error but nothing is saved.
If I use a LNQ query that select a known class object, that's to say if I use a query like this:
var USERS_LOCATIONS2 = from u in dc.USERS select u;
Everything was saved succsessfully. Why?
In thi case, can I configure an unbound column to show data for "Locations.Name", exploiting he FK relation in some way ?
Thanks
Denis Colla
I think there is no bug here, it is by design.
Linq2Sql has a change tracker that listens to field updates and entity insertion or deletion, but it listens only to the original collections and objects being tracked like EntitySets and their entities.
If you make a query and bind the queryResult.ToArray() to the grid, you'll get update tracking but not deletion or insertion tracking. If you create a new object (anonymous or not) in the query, you won't get any tracking.
In any case, when you use anonymous objects, you should you ToArray and skip the lazy load of Linq (not only Linq2Sql), because it can cause some problems as you can see here:
http://community.infragistics.com/forums/p/19436/70300.aspx#70300
Normal 0 21 false false false MicrosoftInternetExplorer4
I got the same problem In the past. I think problem is with LINQ2SQL and not with WinGrid.
When we are collecting data from DataContext LINQ2SQL is managing everything related to database modifications ( updates, deletes, inserts ).
In your first example everything comes from one table and LINQ2SQL is properly binding everything with this.
In your second example you are collecting information one by one ( it might be that your data comes from a different tables ). In this case LINQ2SQL is getting crazy/lazy and can’t do anything to properly save data. This scenario is working Ok when you are only using data to show but not modify.
Maybe I am wrong but so far I couldn’t find any solution ( easy ) to fix this.
My solution is to manually manage and collecting data on the grid using Unbound Columns.
I your scenario it will be first example and manually added column to the grid. We will populate this column in a code after we will retrieve all data from database based on [ from u in dc.USERS select u ].
This solution is not so fast ( in my opinion ) but covers all my requirements.
Regards
Piotr
Thanks for your reply, Charly!
The problem using a "shaped selection" is when you try to call the SubmitChanges() method of the DataContext.
No error occurs, but the changes are not saved on the DB.
Denis
Hello.
I have created a sample using LINQ to Objects based upon the tables that you have given above. I did not have a Sql database to query, so I created two lists of objects and joined them together with an inner join based upon this MSDN article:
http://msdn.microsoft.com/en-us/library/bb397941.aspx
The grid shows all of the columns without the need to create anything unbound. Try an object structure like this and see how ti works for you.