【问题标题】:LINQ Combine two tablesLINQ 合并两个表
【发布时间】:2013-08-09 14:22:14
【问题描述】:

目前我有 2 个数据库表,如下所示:

---------------        --------------------
|  Categories |        |  Item_Categorys  |
---------------        --------------------
|      id     |        |        id        |
|     Title   |        |   Category_ID    |
---------------        |      Item_ID     |
                       --------------------

我有一个模型,它在我的视图上显示复选框,就像

LocalCategoryModel
-------------------
int categoryid
string category_title
bool ischecked

我正在尝试从表中获取所有项目类别,然后获取所有类别行,然后交叉检查它们是否存在类别项目,它会将其放入 IEnumerable 中。所以最后,LocalCategory 拥有所有类别,然后根据 Item_Categorys sql 表中是否有行,将 ischecked 设置为 true 或 false。

【问题讨论】:

    标签: asp.net-mvc linq


    【解决方案1】:
    public class LocalCategoryModel
        {
            public int categoryid { get; set; }
            public string category_title { get; set; }
            public bool ischecked { get; set; }
        }
    
    public IEnumerable<LocalCategoryModel> getSourec()
            {
                IEnumerable<LocalCategoryModel> query = from tbcat in Categories
                            join tbitem_cat in dc.Item_Categorys
                            on  tbcat.id equals tbitem_cat.Category_ID into ct
                            from tbitem_cat in ct.DefaultIfEmpty()
                            select new LocalCategoryModel
                            {
                                categoryid = tbcat.id,
                                category_title = tbcat.Title,
                                ischecked = tbitem_cat == null ? false : true
                            };
    
                return query;
            }
    

    【讨论】:

    • 这正是我想要的!非常感谢!您知道任何资源来熟悉这些类型的查询吗?
    【解决方案2】:

    我给你举个例子。

    它可以帮助你。

    我指的是一本名为“简而言之 C# 4.0”的书。


    // outer collection
    customers.Join (
          purchases,                  // inner collection
          c => c.ID,                  // outer key selector
          p => p.CustomerID,          // inner key selector
          (c, p) => new { c, p } )    // result selector
      .OrderBy (x => x.p.Price)
      .Select  (x => x.c.Name + " bought a " + x.p.Description);
    

     from c in customers
     join p in purchases on c.ID equals p.CustomerID
     select new { c.Name, p.Description, p.Price };
    

    【讨论】:

      【解决方案3】:
      class Category { public int id; public string Title; };
      class CategoryItem { public int category_id; public int item_id; }
      class Category { public int id; public string title; };
      
      // Create categories list
      var categories = new List<Category>() { new Category { id = 1, title = "First" }, new Category { id = 2, title = "Second" } };
      
      // Create categories items list
      var categories_items = new List<CategoryItem>() { new CategoryItem { category_id = 1, item_id = 2 } };
      
      // All categories in categories_items
      var categories_items_set = categories_items.Select(x => x.category_id).Distinct();
      
      // Final query
      var q = categories.Select(x => new { categoryid = x.id, category_title = x.title, ischecked = categories_items_set.Contains(x.id) });
      q.ToList()
      
      // output
      categoryid = 1, category_title = First, ischecked = True
      categoryid = 2, category_title = Second, ischecked = False
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-23
        • 1970-01-01
        • 1970-01-01
        • 2016-09-12
        相关资源
        最近更新 更多