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
  • How do you return a group new {} linq query

    csharp database linq tutorial question
    8
    0 Votes
    8 Posts
    0 Views
    N
    The type of var is an anonymous type. You can not return values of anonymous type from functions in C# 3.0. You should be able to mouse-over q in visual studio and see it's 'real' type (which you can define as your return type), but given your code, the value of the group is an anonymous class, which you can't use if you want a strongly-typed return value. I think this should work though: public class GroupInfo { public string Description { get; set; } public object FieldValue { get; set; } } public List<IGrouping<string, GroupInfo>> Custom_GroupedAFVList(Int32 intSPIDItemID) { var q = from c in new context.vw_AFValueGridListing where c.SPIDID == intSPIDItemID orderby c.DisplayOrder group new GroupInfo() { Description = c.Description, FieldValue = c.FieldValue } by c.GroupName; return q.ToList(); } I would think a Dictionary<K,List<V>> would be easier to work with as a result in a scenario like this than an IEnumerable<IGrouping<K,V>> though, and it's not hard to change to that: public Dictionary<string, List<GroupInfo>> Custom_GroupedAFVList(Int32 intSPIDItemID) { var q = .... return q.ToDictionary(i => i.Key, i => i.ToList()); } :) Niladri Biswas
  • Need Help in LINQ

    csharp linq help question
    3
    0 Votes
    3 Posts
    0 Views
    L
    Some other tools that will help you alonr the way: LINQ Tools[^] Manuel Abadia's Debugger Visualizer[^] ScottGu's Blog on MS LINQ Visualizer[^] Charlie Calvert's Blog on Lambda Expressions and Visualizer[^]
  • How to pass querytring value in LINQ

    csharp database linq tutorial
    4
    0 Votes
    4 Posts
    0 Views
    U
    The code you've typed looks fine to me. The only thing I can think of is the data-type of the querystring. It seem to me that the data-type of EmployeeID is int, so equating it with a hardcoded 2222 is working fine, though if there was a datatype mismatch, I'd expect the compiler to complain. What happens when you use the querystring instead of the hardcoded value?
  • Indexes When Using CreateDatabase()

    database csharp linq com tools
    1
    0 Votes
    1 Posts
    0 Views
    No one has replied
  • 0 Votes
    6 Posts
    0 Views
    L
    Are you binding the query directly to the grid's DataSource? Try using an intermediary datasource like a BindingList or some other DataSource (I cant recall what they are called in ASP.NET). xacc.ide IronScheme - 1.0 beta 3 - out now! ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
  • SequenceEqual and LINQ

    question csharp linq help
    5
    0 Votes
    5 Posts
    0 Views
    N
    :) Niladri Biswas
  • Problem with quering and aggregation of DataTable

    csharp database linq json help
    2
    0 Votes
    2 Posts
    0 Views
    N
    The range variable g has a property Key that represents the key that you have grouped by. In your case, this is b.Field<decimal>("ACCOUNT_ID"). So, if you want to have the account_id with the results, you need this query: var balances = from b in dtAccounts.AsEnumerable() group b by b.Field<decimal>("ACCOUNT_ID") into g select new { accountId = g.Key, credit = g.Sum(b => b.Field<decimal>("CREDIT")), debit = g.Sum(b => b.Field<decimal>("DEBIT")) } Hope this helps Vote me :) Niladri Biswas
  • Linq to sql date format problem

    database tutorial csharp linq help
    2
    0 Votes
    2 Posts
    0 Views
    N
    You can get a string representation of a date by using ToString() myDate.ToString() and a date representation of a string by parsing it. DateTime.Parse(myDateString) You just need to include your conversion in your select clause. from myRecord in myDataContext.MyTable select new {myStringDate = myRecord.myDate.ToString()}; Hope it helps :) Vote me Niladri Biswas
  • LINQ to dataview........

    csharp database linq algorithms
    2
    0 Votes
    2 Posts
    0 Views
    M
    As your starting point DetailContext.Employees is probably a class I think you are in trouble. If you use a datatable/viw as your starting point and AsEnumerable then you can cast the result as a dataview. I do that in this article[^] These look promising[^] but I'll bet they all start from tables Never underestimate the power of human stupidity RAH
  • Treeview SelectedNodeChanged fires for only....

    graphics design sysadmin help question
    3
    0 Votes
    3 Posts
    0 Views
    N
    Hi man, try with IValue convertor. Search google with that and also in code project itself you will find some great examples almost precisely the same as what you are looking for. Good luck. :) Niladri Biswas
  • join query with group and condition

    csharp database linq
    3
    0 Votes
    3 Posts
    0 Views
    N
    from a in Accounts join ed in EntryDetail on a.ACODE equals ed.ACODE join e in Entry on e.EID equals ed.EID where e.EDATE > DateTime.Parse("1/1/1990") group ed by ed.ACODE into g select new { g.Group.Sum(s => s.EDAMOUNT) }; Please vote me :) Niladri Biswas
  • 0 Votes
    7 Posts
    1 Views
    V
    Any? bool result = !collection.Any(e=> e==?? && e==?? );
  • 0 Votes
    1 Posts
    0 Views
    No one has replied
  • 0 Votes
    2 Posts
    0 Views
    I
    The DataGridView will be in ReadOnly mode when you use anonymous types. So what I recommend is defining only those 2 columns in the DataGridView and set AutoGenerateColumns to false and go ahead and use select p;
  • Linq query to get all the records

    csharp database linq question
    3
    0 Votes
    3 Posts
    0 Views
    N
    Here is the answer to your question Everything is correct except that you need to type cast to the particular Entity before you r binding the source to your grid. e.g. public List<Customers> FetchRecord() { return( from p in entities.Customers select p ) } this.Gridview1.DataSource = FetchRecord(); this.GridView1.DataBind(); Hope this helps :) Niladri Biswas
  • Linq Queries

    csharp linq question announcement
    2
    0 Votes
    2 Posts
    0 Views
    N
    It is very easy to do. In such situations, any kind of entity framework will help you. e.g. Ado.net Entity Framework, n-hibernate. I am presenting a sample pseudo code for that in Ado.net Entity Framework A) Fetch operation public List<Employee> FetchRecord() { EmployeeInfoEntities objEmployeeInfoEntities = new EmployeeInfoEntities(); ObjectQuery<Employee> employeeQuery = objEmployeeInfoEntities.Employee; return employeeQuery.ToList(); } B) Insert operation public void AddEmployee(Employee e1) { try { EmployeeInfoEntities objEmployeeInfoEntities = new EmployeeInfoEntities(); objEmployeeInfoEntities.AddToEmployee(e1); objEmployeeInfoEntities.SaveChanges(); } catch (Exception e) { string msg = e.Message; } } c) Update Operation public void UpdateEmployee(Employee e1) { try { EmployeeInfoEntities objEmployeeInfoEntities = new EmployeeInfoEntities(); Employee e2=( from emp in objEmployeeInfoEntities.Employee where emp.EmployeeID == e1.EmployeeID select emp ).First(); e2.Name = e1.Name; e2.Title = e1.Title; e2.Gender = e1.Gender; objEmployeeInfoEntities.SaveChanges(); } catch (Exception e) { string msg = e.Message; } } D) Delete operation public void DeleteEmployee(Employee e1) { try { EmployeeInfoEntities objEmployeeInfoEntities = new EmployeeInfoEntities(); Employee e2 = ( from emp in objEmployeeInfoEntities.Employee where emp.EmployeeID == e1.EmployeeID select emp ).First(); objEmployeeInfoEntities.DeleteObject(e2); objEmployeeInfoEntities.SaveChanges(); } catch (Exception e) { string msg = e.Message; } }
  • WriteXml() Problem

    database xml help question announcement
    2
    0 Votes
    2 Posts
    0 Views
    P
    See title. "WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes. My blog | My articles | MoXAML PowerToys | Onyx
  • Linq contains/split problem

    database csharp linq data-structures sales
    1
    0 Votes
    1 Posts
    0 Views
    No one has replied
  • LINQ & Views

    database csharp linq business json
    1
    0 Votes
    1 Posts
    0 Views
    No one has replied
  • Grouping problem

    help database csharp linq learning
    2
    0 Votes
    2 Posts
    0 Views
    T
    OK, I found my problem and got it to work. In case someone else has the same problem, here is the solution that worked for me: Dim menu = From m In db.ProductCategories _ Select New With {.Title = m.CategoryName, _ .SubCategories = From s In m.ProductSubCategories _ Where s.MainCategoryID = m.ID _ Select s.SubCategoryName, s.ID} Thanks again