Skip to content

LINQ

LINQ (all flavours)

This category can be followed from the open social web via the handle linq-e04cf296@forum.codeproject.com

783 Topics 2.8k Posts
  • 0 Votes
    7 Posts
    2 Views
    H
    OK I think I understand.. user can select multiple elements? The IN(...) clause in SQL can be created using an array or list and .Contains method // list of locations to match - use your dropdown for this var locations = new String() {"aa", "bb", "cc"}; // query var query = from x in db.Table where items.Contains(x.Location) && name = "Sekhar" select x.Name, x.Item 'Howard
  • return type problem

    database help
    4
    0 Votes
    4 Posts
    3 Views
    H
    LINQ querues return IEnumerable(of T) In your example you're creating an anonymous type[^], so T is an anonymous type, so passing it as a return of a function is going to be an issue. Object will work of course, but how do you get the results? 1) don't use an anonymous type, use a Structure or Class e.g. Select New MyType With { .field1 = p.name , .field2 = c1.name1 } 2) return an anonymous type and use reflection to get the property values This is what I do for my Report generator - it does not know the type the report will generate, but it gets a list of properties from the report and uses Reflection. 'Howard
  • 0 Votes
    4 Posts
    4 Views
    H
    To be able to insert in the code you need to know the type of object in your list. Your original query returns IEnumerable(of CP_PRJ_ProjectStatus) so you'd have to insert a new CP_PRJ_ProjectStatus entry into that (which you probably don't want to do). Secondly the query is returning the whole table when only two values are needed. You could create your own structure or class to hold the ID,Name values and then insert one at the top, in the example below I've used the generic KeyValuePair class since it's there already. I've put ID into Key and Name into Value. Public Function ReturnProjectStatus() As List(Of KeyValuePair(of String, String)) Dim Baseclassobject As BaseClassDataContext = Nothing Try Baseclassobject = New BaseClassDataContext(BLLmdlCommon.strConfiguration) Dim query = From p In Baseclassobject.GetTable(Of CP\_PRJ\_ProjectStatus)() \_ Select New KeyValuePair(of String, string)(p.ID, p.Name) \_ Return query.ToList() Catch ex As Exception End Try End Function Private Sub abc() cmbStatus.DisplayMember = "name" cmbStatus.ValueMember = "ID" Dim query = ReturnProjectStatus() 'Insert new value at top query.Insert(0, New KeyValuePair(of string, string)("ID","Value") combobox1.datasource = query End Sub 'Howard
  • Problem in StoredProcedure for LINQ

    help database csharp sharepoint linq
    9
    0 Votes
    9 Posts
    5 Views
    H
    See my reply here[^] 'Howard
  • LINQ and GridView Paging...

    help csharp database linq sysadmin
    2
    0 Votes
    2 Posts
    3 Views
    H
    I think the error is self-explanatory. The gridview does not have a way to page because the datasource isn't pageable.. If paging is required, databind the gridview to an ObjectDataSource. Then write a paged select and pagecount function for your data. You'll need to define a class or struct to hold the resultset as well.. e.g. [ DataObject(true)] public class MyDataSource { [DataObjectMethod(Select)] public static *somereturntype* SelectPaged( *params*, int startRowIndex, int maximumRows ) { // do paged select } public static int SelectRowCount( *params*, int startRowIndex, int maximumRows ) { // compute total rowcount } } 'Howard
  • 0 Votes
    5 Posts
    5 Views
    H
    I did want the XML, but from the code you posted it looks like the LINQ SP function is correctly formed. The problem here is that you are assuming that a SP returns the same as a LINQ table entityset. It does not, so .ToList() won't work. Stored Procedures return a System.Data.Linq.ISingleResult object. To convert this into the rows you expected, use this code: 'Get the ISingleResult from function Dim resultset = c.db.phy_data_onmonth(YearVal, MonthVal) 'Get the collection of values Dim d As List(Of phy_data) = resultset.GetResults(of phy_data) 'Howard
  • LINQ and Parameterized Queries

    database csharp linq sysadmin tutorial
    4
    0 Votes
    4 Posts
    2 Views
    A
    Brilliant, thank you both.
  • Sql query corresponding to LINQ

    csharp database linq
    4
    0 Votes
    4 Posts
    3 Views
    H
    I assumed that ID was not a nullable value (if it truly is an ID it should not be). LINQ is saying it's not sure which extension method to use with ID since it's not an Int but Int? (nullable int). If you don't want to include the nulls, just cast it to an int: dim query = From T In db.Table _ Group T by Name = T.Name _ Into c=Count(), MaxID=Max(Ctype(T.ID,Integer)) _ Where c > 1 _ Select MaxID, Name Or you can convert the nulls to a zero, e.g. MaxID=Max(T.ID.GetValueOrDefault(0)) 'Howard
  • LINQ over DataSet

    csharp database linq xml help
    2
    0 Votes
    2 Posts
    2 Views
    J
    How about something like this... var query = from a in ds.Tables[0].AsEnumarable() where a.Field("CaseId") == txtCaseId.Text && a.Field("Pwd") == txtPassword.Text && b.Field("CaseId") == txtCaseId.Text select new { CaseID = a.Field("CaseId"), CaseManager = a.Field("CaseManager"), NoteCreated = b.Field("NoteCreated"), }
  • LINQ Caching help

    database linq csharp performance help
    2
    0 Votes
    2 Posts
    2 Views
    H
    Without much knowledge of the context it's hard to be specific in helping. - Is tFailureData a LINQ to SQL table? a view? - Is it millions of records? Is it indexed on the search field? - What do RecoveryTotals, RecoveryAllTotals ... do ? - Is there any lazy loading going on? Performance problems like this probably indicate an inefficient design - one that is repeatedly querying the database for lots of small amounts of data, instead of one big query. Use DataContext.Log = Console.Out and see how much SQL is being executed. You might be better to preload all the data (depending on the total data set size) and THEN do the .Where() clause on the in-memory data. e.g. // force SQL to load *all* data first IQueryable<_FailureData> allData = (from tempFailure in tFailureData).ToList(); do{ IQueryable<_FailureData> tdata = from tempFailure in allData .Where(cBaseFilters.GetDateFilter(tDate)) If there are related tables to FailureData e.g. child tables, you may be causing lazy loading to fetch these for each row - e.g. 1000 rows = 1000 SQL queries. Use DataLoadOptions to prefetch if this is the case. 'Howard
  • 0 Votes
    6 Posts
    6 Views
    H
    maybe the SP is returning the record count? 2) try delete and reimport the SP from the database to ensure the definition is correct 'Howard
  • Usage of In predicate in linq vb.net

    csharp linq tutorial question
    2
    0 Votes
    2 Posts
    2 Views
    H
    There isn't an In predicate in LINQ. I assume you mean the IN clause from SQL. e.g. SELECT * FROM Customers WHERE Country IN ('UK','Germany'); There are several ways to do this. The simplest: Dim countries = new String() {"UK","Germany"} Dim query = from c in db.Customers _ where countries.Contains(c.Country) _ select c Note that LINQ to SQL translates the .Contains() function into an IN clause. 'Howard
  • Why did an exception occur?

    question
    2
    0 Votes
    2 Posts
    3 Views
    H
    Your expression xmlTree1.Element() is null. Read the documentation for .Element method "Gets the first (in document order) child element with the specified System.Xml.Linq.XName." 'Child11' is not a child element of 'Root' - it's an element of 'Child1' Try this instead: var child1 = xmlTree1.Element("Child1"); var child11 = child1.Element("Child11"); child11.AddAfterSelf(xmlTree2); 'Howard
  • How can I do this by LINQ?

    question csharp linq xml
    3
    0 Votes
    3 Posts
    3 Views
    M
    Nice response. Thank you my friend.
  • 0 Votes
    3 Posts
    3 Views
    E
    Thank you for your excelent solution.
  • left outer joins

    csharp linq
    3
    0 Votes
    3 Posts
    2 Views
    A
    like sql s server
  • Dynamic Grouping

    linq question csharp database
    2
    0 Votes
    2 Posts
    3 Views
    H
    Not an expert on the Dynamic Queries lib (although I have looked at it). I assume that "new (SalePrice, SaleCount)" is the result generator so I would assume that t is an anonymous type containing a .SalePrice and .SaleCount property - you might be able to read the values using reflection and PropertyInfo. 'Howard
  • Seraching in XML by LINQ ?

    csharp php database linq com
    9
    0 Votes
    9 Posts
    4 Views
    M
    Thanks again my friend.
  • 0 Votes
    2 Posts
    2 Views
    H
    I assume this is a Linq to SQL question - and that you are using SQL datasource? 1) databind both dropdowns to a LINQdataSource 2) the LINQdataSource for Country dropdown is tblCountry 3) the LINQdataSource for District dropdopwn is tblDistrict 4) add a WHERE clause to the District LinqDataSource: WHERE [Country] == [control] - [CountryDropdown] 5) make the country dropdown AutoPostback=true - so when a user changes the country, the District is repopulated. ASP handles the dependencies. Read this [^]for more info. 'Howard
  • deleting the miltiple records vb.net 3.5

    csharp database linq
    2
    0 Votes
    2 Posts
    2 Views
    H
    Without using loops this can be done: var IDs = @"1,2,3,4,5,6"; var IDarray = IDs.Split(","); var toDelete = from T in db.Table where IDarray.Contains(T.ID) select T; db.Table.DeleteAllOnSubmit(toDelete); db.SubmitChanges(); Unfortunately this results in six delete commands on the server, so it isn't as efficient as a single SQL delete command. Other people have been developing extensions to LINQ for deletes and updates which you could try out. See Aney's blog[^] or Zhao's blog[^] for more info. 'Howard modified on Saturday, August 9, 2008 10:59 AM