Sai Stuff to Developers

April 12, 2012

Adding a new record to an IQueryable variable

Filed under: LINQ — tosaik @ 7:09 am
Tags: ,

Hi All,

Recently working on one of my application using Linq, i need to fill the datagridview with selected rows and columns which i can get from querying a list of objects using Linq. Now for every result i need to add an additional static record to the grid view. for this i need to have this record as a last record for my selected IQueryable variable from LINQ result.  Let me show you through example what i said till now…

Ex:

var varresult = from r in lstobject select new { COLUMN1 = r.Title, COLUMN2 = r.Price };

DataGridView1.datasource = varresult ;

DataGridView1.databind();

Now i need to add a new static record (i.e.., new{COLUMN1=”Discount Price”, COLUMN2=10}) to the existing varresult object…

varresult  = varresult.ToList().Union(new[] { new{COLUMN1=”Discount Price”, COLUMN2=10}});

DataGridView1.datasource = varresult ;

DataGridView1.databind();

 

thats it Now a last record will be what you want… Please note that the variables and there types should be same as result object had.

Hope this Helps u…

 

Happy Coding 🙂

 

 

 

 

 

NHIBERNATE: ILLEGAL ATTEMPT TO ASSOCIATE A COLLECTION WITH TWO OPEN SESSIONS

Filed under: DotNet,NHibernate — tosaik @ 4:55 am
Tags: , ,

Hi All recently i got this issue when iam working on my application which is using NHibernate as the OR Mapper, and i seriously work around about this issue and got the tail of this issue.

This issue raised because i get an model object which had lazy load option true and modified the same object and trying to save/update the same object.. this is fine from our side but what accually happening inside the Nhibenate is due to lazy load the model object still running on a Nhibernate session, again when we say save the same object it created another session to save it as current context session is already running to fetch the model object.. so NHibernate doesnt accept an object running on two or more sessions.

Solution: Solution is simple we need to manage or code what ever the word.. try to have a single session always active..
for this we need to change a little coding as shown here.

private static ISession _mySession;

public static ISession mySession
        {
            get {
               
                if (!CurrentSessionContext.HasBind(SessionFactory))
                    CurrentSessionContext.Bind(SessionFactory.OpenSession());
                
                _mySession = SessionFactory.GetCurrentSession();
                return _mySession;
            }
        }

Here in the above couple of lines of code says to the NHibernate that if any session is aready bind to the Current Context then use the same session and if not create one session and bind the same to the Current Context.

Hope this Helps you….

Happy Coding 🙂

 

Blog at WordPress.com.

%d bloggers like this: