LINQ和Lambda,基于数组

时间:2016-01-12 20:39:49

标签: sql linq lambda

我有这个模型,我想编写一个where子句来查询基于和数组的特定结果,例如我想只显示数组中包含id的歌曲[1,3,7,8]: 我写了下面的表达式,但我不知道怎么写where语句:

var model = from c in _db.Categories
                from co in _db.Composers
                from k in _db.Keys
                from p in _db.Poets
                from si in _db.Singers
                from t in _db.Types
                join s in _db.Songs on
                    new
                    {
                        Catid = c.id,
                        Comid = co.id,
                        Keyid = k.id,
                        Poetid = p.id,
                        Singerid = si.id,
                        Typeid = t.id
                    }
                    equals
                    new
                    {
                        Catid = s.CategoryId,
                        Comid = s.ComposerId,
                        Keyid = s.KeymId,
                        Poetid = s.PoetId,
                        Singerid = s.SingerId,
                        Typeid = s.TypeId
                    }
                where
                      ............
                        select new SongViewModel
                        {
                            id = s.id,
                            Name = s.Name,
                            Lyric = s.Lyric,
                            Chord = s.Chord,
                            Note = s.Note,
                            Audio = s.Audio,
                            Lycho = s.Lycho,
                            Likes = s.Likes,
                            Dislikes = s.Dislikes,
                            Category = c.Name,
                            Composer = co.Name,
                            Keym = k.Name,
                            Poet = p.Name,
                            Singer = si.Name,
                            Type = t.Name
                        };

1 个答案:

答案 0 :(得分:2)

试试这个:

var ids = new List<int> {1,3,7,8};

  ...
where ids.Contains(s.Id)
select
  ...