0

カテゴリのリスト(CategoryID)リストcategoryIdsがあります。このリストには、ユーザーが以前に選択したものに基づくIDが含まれています。

次に、1つ以上のカテゴリのメンバーになることができる会社のデータベースがあります。これは結合テーブルCompaniesInCategoryで維持され、company.Categoriesのようなオブジェクト構造になります。

今私の質問は、選択したカテゴリの1つに少なくともメンバーであるすべての会社をどのように選択するかです。

List<int> categoryIds = new List<int>() {123, 3, 5858, 23};    
List<company> companies = (from c in context.Companies
                           where c.Categories.(one of the Id's of these c.Categories should be in the list named categoryIds) 
                           select c);

各企業には、カテゴリのリストが添付されています。そして、このカテゴリのリスト(c.Categories)(すべてCategoryIdを持っている)から、少なくとも1つはリストcategoryIdsのIDの1つと一致する必要があります。

4

5 に答える 5

1

あなたはこれについて話しているようです:

var categoryIds=(new[] { 123, 3, 5858, 23 }).ToList();

var category=
    new {
        Id=123
    };

var company=
    new {
        Categories=(new[] { category }).ToList()
    };

var context=
    new {
        Companies=(new[] { company }).ToList()
    };

var companies=(
    from c in context.Companies
    from x in c.Categories
    from y in categoryIds
    where x.Id==y
    select c
    ).ToList();

したがって、指定した場所:

where c.Categories.(one of the Id's of these c.Categories should be in the list named categoryIds)

だろう:

where c.Categories.Any(x => categoryIds.Contains(x.Id))

companiesは次と同等であるためです。

var companies=(
    from c in context.Companies
    where c.Categories.Any(x => categoryIds.Contains(x.Id))
    select c
    ).ToList();
于 2013-01-07T02:29:21.330 に答える
1
var companies = dc.Companies
    .Where(c => c.Categories.Any(cat => categoryIds.Contains(cat.Id)))

「少なくとも 1 つ」は、Any()多くの場合、LINQ のメソッドに最適に変換されます。

于 2013-01-07T02:24:27.847 に答える
0
List<int> categoryIds = new List<int>() {123, 3, 5858, 23};    
List<company> companies = (from c in context.Companies
                           where categoryIds.Contains(c.Categories)
                           select c).ToList();  //I would add .ToList() since List<company>

これはうまくいくはずです

リストが実際には4intしかない場合は、次のようなこともできます。

where c.Categories == 123 || c.Categories == 3 || //ect.
于 2013-01-07T00:54:27.353 に答える
0

あなたの問題を理解しているので、(DB)Category Table と Category List を確認する必要があります。次に、メンバーであるすべての会社を取得するのは、Category テーブルです。

List<int> categoryIds = new List<int>() {123, 3, 5858, 23};    
List<company> companies = (from c in context.Companies
                           from b in context.Categories //Asuming there is a index table of some sort
                           where categoryIds.Contains(b.CatID) && c.companyID == b.companyID
                           select c).ToList();
于 2013-01-07T02:16:56.517 に答える
0

私は以前ほど内包構文に精通していませんが、カテゴリ フィールドの名前がCategoryIdであると仮定すると、ラムダ バージョンは次のようになると思います。

var companies = dc.Companies
    .Where(c => categoryIds.Contains(c.CategoryId))
于 2013-01-07T00:57:37.080 に答える