C# - LINQ To SQL Dynamic Select?
Jun 7, 2010
how to indicate which columns I would like returned at run-time from a LINQ To SQL statement?I am allowing the user to select items in a checkboxlist representing the columns they would like displayed in a gridview that is bound to the results of a L2S query.I am able to dynamically generate the WHERE clause but am unable to do the same with the SELECT piece. Here is a sample:
var query = from log in context.Logs select log;
query = query.Where(Log => Log.Timestamp > CustomReport.ReportDateStart);
query = query.Where(Log => Log.Timestamp < CustomReport.ReportDateEnd);
[code]...
View 2 Replies
Similar Messages:
Apr 20, 2010
I've added the Dynamic.cs file to my project and have set it's Build Action = Compile in order to get the System.Linq.Dynamic namespace.
However, when I try to use any of the new code I am getting an ambiguous call error; see below.
[Code]....
View 2 Replies
Dec 24, 2010
We would like to perform something like this
string strCondition = "FirstName=='abc'"
from p in People.Where(strCondition) select p
In our architecture we are usign both Linq-Sql as well Linq-Entity. So please give some thoughts wehter it is possible or not and is there any way to perform this?
View 2 Replies
Aug 17, 2010
What is difference between select { } and select new {} In Linq Query
View 3 Replies
Aug 26, 2010
why use where condition before select statement in LINQ
View 2 Replies
Dec 18, 2010
I am using this C# with linq to sql:
string currentLabel = from s2f in stream2FieldTypesTable
where s2f.s2fID == item.s2fID
&& (s2f.s2fLabel != item.s2fLabel || s2f.s2fIsRequired != item.s2fIsRequired)
select s2f.s2fLabel;
I am getting a compiler error saying i can't assign type System.Linq.IQueryable<string> to string.
I tried this code:
string currentLabel = from s2f in stream2FieldTypesTable
where s2f.s2fID == item.s2fID
&& (s2f.s2fLabel != item.s2fLabel || s2f.s2fIsRequired != item.s2fIsRequired)
select s2f.s2fLabel.ToString();
And that returns the same error. I'm sure this is a simple thing. what am I missing? I just want the first s2fLabel that matches the where clause.
View 4 Replies
Oct 7, 2010
[Code]....
[Code]....
I have to get the maximum of these two select values. The select values are nullable. How do I go about checking if the value is null before calling the Max() method on it? I tried the ?? coalesing operator (example: e.HomeSales.Max() ?? 0M,) but I get the following error:"Operator '??' cannot be applied to operands of type 'decimal' and 'decimal'"
View 2 Replies
Mar 15, 2011
I have several tables to load from depending on a string value using Linq to Sql I currently have
[Code]....
but of course that only gets the specific table DOffices if I wanted to grab tables at runtime what would be the best way to go about it?
View 1 Replies
Jan 30, 2011
i am using stored proc in linq to sql but i have done insert & delete operations but im unable to do the select operation i have tried but no use,whenever i enter the id into the textbox it should display the record?
View 5 Replies
Jan 17, 2011
I have a table created on the fly. The table structure is dynamic.
This is the table structure generated by the end of year 2010
DealerCode, 201010, 201011, 201012
This is the table structure generated by the end of Jan 2011
DealerCode, 201011, 201012, 201101
What I need to do is selecting all data and then bind it into a gridview
To build a LINQ query, we need to drag the table into dbml file first. As we don't know how the table look like, can we add it in dynamically? Are there any other solutions?
View 1 Replies
Sep 20, 2010
I am writing a function in which i generate autocode in specific format i want to make this function generalize so that i send tablename and columnname parameter and function return the next code. I am using linq to sql with MVC e.g. i want to make table name and column name dyanamic
[Code]....
View 3 Replies
Mar 25, 2011
Linq add wildcard in the where clause dynamically I have a query, i.e.
var query = from r in context.company .....
then have some business logic and based on the validation I want to add dynamically a where clause
if(a == "1")
query = query.Where(r=> r.Name = Fullname); //works!!
else if(a == "2)
query = query.Where SqlMethods.Like(r=> r.Name,"%"+Fullname+"%"); //fails to compile , the r.Name is not found..
View 3 Replies
Nov 7, 2010
I'm trying to do something that on the face of it is very simple.
If I have 5 text boxes, and 1 of them is blank, I want to exclude that textbox from the query, rather than searching for where a field is blank.
I've tried to do a bit of research into dynamic linq queries, but have come up empty handed, or what I've found doesn't seem to quite answer my question.
View 3 Replies
Mar 21, 2011
Loading data into gridview, retieving data using linq,I have a method that accepts two paramters that would sort specific field and return the object to the gridviewi.e.
passing into the method GenerateData(string sortExp, string sortDir)
sortExp = "Name", sortDir = "descending"
linq query://works
[code]...
View 2 Replies
Feb 2, 2011
I have a gridview with several columns, 3 of which I'd like to sort. The source for the data is held in the session.
protected void MyGridHistorySort(object sender, GridViewSortEventArgs e)
{
var TheColumn = (e.SortExpression).ToString();
TheDataHistory = (List<ViewDataHistoryModel>)Session["SessionDataHistory"];
var test = "data.DataDate";
var NewDataSource = from data in TheDataHistory
orderby test
select data;
MyGridHistory.DataSource = NewDataSource;
MyGridHistory.DataBind();
DataDate is a valid column in the list but the orderby statement doesn't work. Ideally, I'd like it to sort with the variable TheColumn by writing something like test = "data."+TheColum; and then add a sort direction based on a boolean. I looked at the OrderBy extension method NewDataSource.OrderBy(test); but that doesn't work either.
View 3 Replies
Nov 29, 2010
Is it possible to generate dynamic LINQ statements? I need to be able to build conditional "WHERE" clauses for some of my LINQ statements. I don't want to write a giant block of code to hand every possible combination of filters, sorts and groups. I found a article online that outlines how to use string based where clauses here. Does LINQ in framework 4.0 no longer support this? Every example I've found has giant walls of virtually unreadable, must likely unmaintainable code.
View 1 Replies
Jun 14, 2010
I'm building an ASP.NET MVC site that uses LINQ to SQL. In my search method that has some required and some optional parameters, I want to build a LINQ query while testing for the existence of those optional parameters.
Here's what I'm currently thinking:
using(var db = new DBDataContext())
{
IQueryable<Listing> query = null;
//Handle required parameter
query = db.Listings.Where(l => l.Lat >= form.bounds.extent1.latitude && l.Lat <= form.bounds.extent2.latitude);
//Handle optional parameter
if (numStars != null)
query = query.Where(l => l.Stars == (int)numStars);
//Other parameters...
//Execute query (does this happen here?)
var result = query.ToList();
//Process query...
Will this implementation "bundle" the where clauses and then execute the bundled query? If not, how should I implement this feature?
View 1 Replies
Sep 6, 2010
i have an problem to use of PATINDEX function in select clause with Linq.
View 1 Replies
May 11, 2010
If I have a linq statement like:
[Code]....
There may be a lot of data in the Blogs table. What if all I wanted was say BlogID, BlogTitle and BlogAuthor and nothing else, is there a way to do this in an effort to make the sql more efficent?
Along the same lines in my DataContext (.dbml) file I added some associations between some of my tables. This makes coding nice when I do a linq statement that will join those tables automatically for me but my concern is does it do that everytime, thus making all my linq quieries inefficient?
View 1 Replies
Aug 18, 2010
My application includes BUILDING table with columns BUILDING_ID, NAME, TOWN_ID, I run the LINQ query listed below and need to append two letters to buildings.NAME based on TOWN_ID. I assume that I need to place an "if" or "select" statement inside the query but I can not figure out how to do that,
[Code]....
View 4 Replies
Jun 24, 2010
I have the following one-to-many relation between two objects.
Parent
--> IList<Child>
Now, I have a List of Parent objects and I want the First Child of each parent in the list.
What is the best way to do this using Linq?
View 3 Replies
May 17, 2010
i have 2 table missions and missiondays with a relationship 1..n
I want select all missions that has all missiondays with an attribute called "visible" = true
I've tried with:
db.TDP_Missioni.Include("TDP_MissioniDestinazioni").where(p => p.TDP_MissioniDestinazioni.visible == true)
But there is an error. Indeed from p.TDP_MissioniDestinazioni i don't see the table's attributes. I think it's because the relationship is 1..n.
View 2 Replies
Sep 21, 2010
With linq to entities, how can I delete and update data inside a database without first selecting the complete entity at first?
View 4 Replies
Nov 25, 2010
I have a page with filters and accordingly to what the user selects, it has to generate a query. I'm using this code:
[code]....
However Linq-To-Sharepoint doesn't convert most of this to CAML and the list I'm querying has more than 50000 items. It takes about 4-8 seconds to retrieve the items which is not acceptable.
I've been trying to generate a dynamic query but so far I haven't been able to get it to work.
With AND and OR operations to generate the query I could put all those conditions on code and increase performance.
View 1 Replies
Sep 1, 2010
creating a dynamic select statement, so far my SP look like below
[code]....
View 4 Replies