.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links


Posted By:      Posted Date: September 29, 2010    Points: 0   Category :ASP.Net

I need to execute Linq to SQL queries and return results of IQuerabyle<T>.  The queries are complex joins and/or unions which do not match any domain class defined in the dbml.  Is it possible to create a dynamic class in Linq select expression without specifying a class that matches the structure of the query?  I have many queries and views that I use to retrun IQuerable, but I don't want to define custom classes for each query result.  This is a mock up of what I would like to have

public IQueryable<ObjectCreatedDynamicly> Test()
            var myTestObj = (from v in ComplexJoinsOrUnions()
                             select new ObjectCreatedDynamicly{
                                 userFirstName = v.FirstName,

View Complete Post

More Related Resource Links

how do I pass parameters to the linqdatasource selecting event handler? can I pass an IQueryable?



I'm having an issue coming up with an appropriate code design to implement a search page using linq to sql for my DDL. I think the best way for me to explain where I'm at is describing what I've done in the past, before linq. Lets say we have a basic search page with various user input controls for search options... for simplicity, let's say there are two main ways the user can search. 1. a dropdown filled with account numbers. When that drop down is changed and an account number is selected, a gridview needs to be populated with all the records for that account. 2. the second way is a dropdown for choosing what field you want to search, a textbox for entering what you want to search for, and a button to execute that search. In the past, just using straight ado.net I would handle each of these two search options separately within the events of the dropdown list and the button, respectively. In selectedIndexChanged for 1, and in the buttonClick event for 2, I would construct my sqlCommand etc, ultimately returning a dataset or datatable which I would then bind to the gridview. The logic was separated by the two different event handlers. Now, fast forward to linq to sql and I have a similar page layout, but I am using the linqdatasource selecting event to build my where conditions... now the difference is *everything*, all the logic, is now in this

Creating Dynamic Expressions for Lambda IQueryable Expressions



I have a ListBox that contains userNames which is inputted by a user during run time. This number of items in the ListBox is dependent on the user's input. These userNames will then be used in a Lambda expression like this:

var vHandleResult = vHandleResult.Where(a => a.UserName.Contains("userName"));

This works fine when there is only 1 user in the ListBox but doesn't produce the correct results when there are multiple items in the listBox. I tried manually creating the expression which should be written as this:

var vHandleResult = vHandleResult.Where(a => a.UserName.Contains("userName1") || a.UserName.Contains("userName2") || a.UserName.Contains("userName3") || a.UserName.Contains("userName4"));

I tried forming the "a.UserName.Contains("userName") expression as a string which is dependent on the number of the items (using foreach) and then just plug in the resulting string in the expression but it seems that it doesnt accept string as an expression.

Can someone help me identify the best approach for this?

Thanks in Advance!


IQueryable, IEnumerable, List, Collection


I am using Interface for Respositary. Someone tell me where these can be best used. Any scenario. Also googling to understand these.
What do we mean by concret class and domain objects?


Saving IQueryable in cache error


Hello, i want to save a IQueryable in cache like this:

                            Dim cachekey As String = "all-ascending" + _from.ToString + _to.ToString + _skip.ToString + _take.ToString
                            If Cache.Get(cachekey) IsNot Nothing Then
                                myQueryOfImages = Cache.Get(cachekey)
                                myQueryOfImages = From _image In myEnt.Images
                                                  Where _image.Id >= _from And _image.Id <= _to
                                                  Order By _image.Id Ascending
                                                  Skip _skip Take _take
                                                  Select _image

                                Cache.Insert(cachekey, myQueryOfImages, Nothing, Date.Now.AddMinutes(10), Caching.Cache.NoSlidingExpiration)
                            End If

However when i try to retreive it and use it i get this error:

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

New IQueryable Support for Http Services in WCF


One of the things that caught my attention when WCF Data Service was released a couple of years ago was the ability of using URI segments for passing queries directly to an underline linq provider. This represented a very powerful feature for allowing clients to filter and manipulate a large data result set on the server side with a simple API and without incurring in any unnecessary performance penalty (the queries are performed and resolved in the data source itself most of the times) and having all that logic implemented in the service itself.

I've always thought that this cool feature should be something that anyone could reuse in any service, and not something available in WCF data services only. For example, If you already had an existing REST service implementation, moving that logic to a WCF data service with OData support was only the option you had if you wanted to reuse that query support.  By moving your service to a WCF Data Service, you were also limiting your service to the content types supported by OData (Xml Atom and JSON), and it would only makes sense with services centered around the concepts of CRUD for manipulating data.

Fortunately, this functionality has been moved to a separated library and released as part of the new WCF Web APIs in wcf.codeplex.com. This means that you can now use the IQ

IQueryable casting to IOrderedQueryable doesn't work !


I am passed an IQueryable that might be already be ordered and that therefore might be an IOrderedQueryable. I need to apply it a new ordering via reflection, anb before choosing if calling either OrderBy or ThenBy I need to now if the Queryable is IOrderedQueryable.

Now comes the problem! To see if may variable say query is an IOrderedQueryable I do:

IOrderedQueryable orderedQuery = query as IOrderedQueryable;

One might expect orderedQuery be null in case query is not an IOrderedQueryable ! INSTEAD orderedQuery is NEVER NULL. It takes the value of a different type!

I understand that this might happen because of deferred execution...

I tried also: query is IOrderedQueryable and it is always true!

How can I verify if query is actually an IOrderedQueryable before getting an exception when calling ThenBy?

The IQueryable comes from the Entity Framework 4.0

I am able to see it if I access the object that is behind the interface, but this way I would loose the benefif of using interface, and my class might not work with a different implementation of the IQueryable.

Thanks in advance

Misunderstanding of Arrays, IEnumerable, IQueryable and Accessing their Values


I have a database and Web Application that I would like to write some logic for.  I would like to:

1) Get an Assessment from the database table Assessment, based upon the AssessmentID ( GetAssessmentByAssessmentID() )
2) Access the "values" returned from that Assessment, to do some calculations 

I don't really understand the Array, IEnumerable, IQueryable interfaces well enough to know what SHOULD be the conventions and methods I should use, and I also cannot access the values that I want to.

For Example:

        protected void Page_Load(object sender, EventArgs e)
            AssessmentsBLL _assessments = new AssessmentsBLL();
            Array userPerformance = _assessments.GetAssessmentByAssessmentID(2).ToArray();

            GridView1.DataSource = userPerformance;

            lblTest.Text += userPerformance.GetValue(0).ToString() + "<br />";

        public IEnumerable<Assessment> GetAssessmentByAssessmentID(int _assessmentID) //Should I Return an IEnumerable<Assessment> here?
            IEnumerable<Assessment> _assessment = _context.Assessments
                .Where((c) => c.AssessmentID == _assessmentID);
            return _assessment;


ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend