Blog posts about linq

  • Unknown server tag 'asp:LinqDataSource'

    June 18, 2012 | Tags: ASP.NET, Database, C#, Troubleshooting, LINQ
    If you're getting an error saying that the asp:LinqDataSource is unknown, you most likely is missing the declaration in your web.config file. To fix this:

    1. Open your web.config file and locate the <controls> block.
    2. Add the following element: <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.UI.WebControls" tagprefix="asp"> into the <controls&gt; block.
    3. Reload the page and see it working :)

  • Get all values from CheckBoxList in C#

    November 04, 2010 | Tags: C#, LINQ, Snippet
    This is a class that add an extension to the CheckBoxList class to return a comma separated string with all the selected values.

    You call this function by the following:
    string mySelectedValues = myCheckBoxList.GetSelectedValues();
    The class to place somewhere in you project (remember to add a using-reference to any class you want to call this function from:
    public static class CheckBoxListExtension {
    public static string GetSelectedValues(this CheckBoxList checkBoxList) {
    string retval = string.Empty;

    try {
    foreach (ListItem item in checkBoxList.Items) {
    if (item.Selected) {
    retval += item.Value + ',';
    }
    }
    retval.TrimEnd(',');
    }
    catch (Exception) {
    }

    return retval;
    }
    }

    Or the same code if LINQ is your poison:
    public static class CheckBoxListExtension {
    public static string GetSelectedValues(this CheckBoxList checkBoxList) {
    string retval = string.Empty;

    try {
    retval = checkBoxList.Items.Cast<ListItem>().Where(item => item.Selected).Aggregate(retval, (current, item) => current + (item.Value + ','));
    retval.TrimEnd(',');
    }
    catch (Exception) {
    }

    return retval;
    }
    }

  • LINQ: Join on multiple conditions

    September 20, 2010 | Tags: LINQ, Snippet
    Here's a sample of a LEFT OUTER JOIN in LINQ using two conditions:
    MyDataContext db = new MyDataContext();
    string username = "test";

    IEnumerable<MyType> query = from c in db.MyCategories
    join s in db.MySettings on new { Username=username, CategoryId=c.CategoryId } equals new { Username=s.UserId, CategoryId=s.CategoryId } into tmp
    from ss in tmp.DefaultIfEmpty()
    orderby c.CategoryName
    select new MyType(c.CategoryId, c.CategoryName, ss.UserId == null);
    Notice the part after on:
    join alias in tablename on new { Name1=alias.Field1, Name2=alias.Field2 } equals new { Name1=alias.Field1, Name2=alias.Field2 }

    Please note that I'm mixing the conditions in my example using one value from a table and one string from the code. It's also important that you set the same names one each side of the equals

  • The best links for learning LINQ

    September 17, 2010 | Tags: Database, C#, LINQ
    The NOT IN clause in LINQ to SQL
    MSDN: Visual Basic LINQ to SQL Samples
    MSDN: 101 LINQ Samples for C#

  • Insert and delete with LINQ to SQL

    September 17, 2010 | Tags: C#, LINQ, Snippet
    Needed to remove all old posts associated with a specific user and then add new posts. With a DataSet, this could be done by fetching the data, clear it and then add the new posts. A simular way is possible in LINQ-to-SQL:
    CalendarDataContext db = new CalendarDataContext();
    List<int> categories = new List<int>() // ..with some data of course

    // Get current posts
    IQueryable<SubscriptionSetting> settings = db.SubscriptionSettings.Where(s => s.UserId == username);

    // Delete all old posts
    db.SubscriptionSettings.DeleteAllOnSubmit(settings);

    // Add new
    foreach (int c in categories) {
    db.SubscriptionSettings.InsertOnSubmit(new SubscriptionSetting() { UserId = username, CategoryId = c });
    }

    db.SubmitChanges();