基于可选搜索参数使用实体框架选择数据

时间:2014-03-02 01:10:25

标签: c# wcf entity-framework visual-studio-2012

我有一个使用EF与数据库连接的项目。它通过WCF Web服务完成此操作。这是我的第一个EF项目,我正在尝试研究如何基于可选参数构建查询。

我的网络服务与EF中的类具有匹配字段的数据协定。 Web服务与数据库联系,填充这些客户端类并将它们传递回客户端。举个例子....

在网络服务中

[DataContract]
public class MyMovie
{
    [DataMember]
    public int MovieId;
    [DataMember]
    public string MovieName;
    [DataMember]
    public int MovieRatingId;
    [DataMember]
    public string MovieRunTime;
    [DataMember]
    public string MovieIMDBUrl;
    [DataMember]
    public DateTime MovieDateAdded;
    [DataMember]
    public List<MyMovieActor> Actors;
    [DataMember]
    public List<MyMovieMedia> MediaFiles;
}
public class MyMovieActor
{
    [DataMember]
    public int ActorId;
    [DataMember]
    public int ActorMovieId;
    [DataMember]
    public string ActorName;
    [DataMember]
    public string ActorCharacter;
}
public class MyMovieMedia
{
    [DataMember]
    public int MediaId;
    [DataMember]
    public int MediaMovieId;
    [DataMember]
    public string MediaFileLocation;
    [DataMember]
    public bool MediaDefault;
}

数据模型(简化)......

enter image description here

我的网络服务方法....

    public MyMovie GetMovie(int movieId)
    {
        MyMovie response = new MyMovie();

        using (MovieDatabaseEntities DbContext = new MovieDatabaseEntities())
        {
            var query = (from oData in DbContext.Movies
                         where oData.MovieId == movieId
                         select oData).ToList();


            if (query.Count > 0)
            {
                response.MovieDateAdded = query[0].MovieDateAdded;
                response.MovieId = query[0].MovieId;
                response.MovieIMDBUrl = query[0].MovieIMDBUrl;
                response.MovieName = query[0].MovieName;
                response.MovieRatingId = query[0].MovieRatingId;
                response.MovieRunTime = query[0].MovieRunTime;
            }

            List<MyMovieActor> actors = new List<MyMovieActor>();
            foreach (MovieActor ma in query[0].MovieActors)
            {
                MyMovieActor a = new MyMovieActor();
                a.ActorId = ma.ActorId;
                a.ActorMovieId = ma.ActorMovieId;
                a.ActorCharacter = ma.ActorCharacter;
                a.ActorName = ma.ActorName;
                actors.Add(a);
            }
            response.Actors = actors;

            List<MyMovieMedia> medias = new List<MyMovieMedia>();
            foreach (MovieMedia mm in query[0].MovieMedias)
            {
                MyMovieMedia med = new MyMovieMedia();
                med.MediaId = mm.MediaId;
                med.MediaMovieId = mm.MediaMovieId;
                med.MediaFileLocation = mm.MediaFileLocation;  //HDD Folder 
                med.MediaDefault = mm.MediaDefault; //Image to show on listing
                medias.Add(med);
            }
            response.MediaFiles = medias;
        }
        return response;
    }

所以现在我要创建

public List<MyMovie> GetMovies(string moviename, int movierating, string movieruntime)

将填充所有参数或零长度字符串。如果他们有值,我想将其添加为where子句的一部分。

所以基本上更新

                var query = (from oData in DbContext.Movies
                         where oData.MovieId == movieId
                         select oData).ToList();

相当于

            var query = (from oData in DbContext.Movies
                         where 1=1
                   if(String.IsSafe(moviename))
                   {
                         && oData.MovieName Like %movieId%
                   }
                   if(movierating != 0)
                   {
                         && oData.MovieRating = movierating
                   }
                   if(String.IsSafe(movieruntime))
                   {
                         && oData.MovieRuntime == movieruntime
                   }
                         select oData).ToList();

考虑到这一点让我更加困惑,我总是使用旧的ADO.NET,DataReaders,Adapters,Stored Procs等编写我的数据层,但是在这个项目中我被要求使用EF。这让我很困惑。他们对老狗和新技巧的看法是什么?!

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:5)

IQueryable是你的朋友。它允许以多个步骤编写查询,但它不会生成SQL并在需要之前执行它(例如由于迭代或ToList()调用)。所以你的代码看起来像是:

var query = (from oData in DbContext.Movies select oData);
if (!string.IsNullOrEmpty(moviename))
  query = query.Where(m => m.MovieName.Contains(moviename));
...