Linq To SQL toList <class> - 无效的转换

时间:2015-07-26 20:15:44

标签: c# sql-server linq

我不确定我做错了什么。但是当我进行toList时,linq to sql正在窒息。

它正在运行select的这个区域。

case RightSearchType.Text:

如果将结果放在var变量中而不使用ToList,那么运行正常就没有问题。

我试过了

ToList()

ToList<URRightsView>()

它们都会产生同样的问题。

我希望在开发人员调用此方法时传回强类型列表。

请记住这不是完成的代码。这是我第一次使用LINQ to SQL和我的第一次测试,看看我是否做得对。

因此失败了。

真正感谢任何解决铸造问题的帮助:)

    public static List<URRightsView> Search(string SearchText, long Id, RightSearchType SearchType)
    {
        List<URRightsView> dRights = null;
        try
        {
            using (DataContext db = new DataContext(AppSettings.Settings.ConnectionString))
            {
                Table<URRights> tRights = db.GetTable<URRights>();
                Table<URRoles> tRoles = db.GetTable<URRoles>();
                Table<URApps> tApps = db.GetTable<URApps>();
                Table<URRightRoleMapping> tMapRoles = db.GetTable<URRightRoleMapping>();
                Table<URApplicationRight> tMapApps = db.GetTable<URApplicationRight>();
                string results = string.Empty;
                switch (SearchType) {
                    case RightSearchType.Application:
                        dRights = (from ma in tMapApps
                                   join apps in tApps on ma.AppFK equals apps.AppId
                                   join r in tRights on ma.RightFK equals r.RightId
                                   where (r.Title.Contains(SearchText) || r.Description.Contains(SearchText)) && ma.AppFK == Id
                                   orderby r.Title, apps.Name ascending
                                   select new URRightsView
                                   {
                                       Title = r.Title,
                                       Classification = r.Classification,
                                       Description = r.Description,
                                       RightId  = URUtil.GetLong(r.RightId),
                                       RightType = r.RightType,
                                       AppName = apps.Name
                                   }).ToList<URRightsView>();
                        break;
                    case RightSearchType.Role:
                        dRights = (from ma in tMapApps
                                     join apps in tApps on ma.AppFK equals apps.AppId
                                     join r in tRights on ma.RightFK equals r.RightId
                                     join mr in tMapRoles on r.RightId equals mr.RightFK
                                     where (r.Title.Contains(SearchText) || r.Description.Contains(SearchText)) && ma.AppFK == Id
                                     orderby r.Title, apps.Name ascending
                                     select new URRightsView
                                     {
                                         Title = r.Title,
                                         Classification = r.Classification,
                                         Description = r.Description,
                                         RightId = URUtil.GetLong(r.RightId),
                                         RightType = r.RightType,
                                         AppName = apps.Name
                                     }).ToList<URRightsView>();
                        break;
                    case RightSearchType.Text:
                        dRights = (from ma in tMapApps
                                    join apps in tApps on ma.AppFK equals apps.AppId
                                    join r in tRights on ma.RightFK equals r.RightId
                                    where r.Title.Contains(SearchText) || r.Description.Contains(SearchText)
                                    orderby r.Title, apps.Name ascending
                                    select new URRightsView
                                    {
                                        Title = URUtil.GetString(r.Title),
                                        Classification = URUtil.GetString(r.Classification),
                                        Description = URUtil.GetString(r.Description),
                                        RightId = URUtil.GetLong(r.RightId),
                                        RightType = URUtil.GetLong(r.RightType),
                                        AppName = URUtil.GetString(apps.Name)
                                    }).ToList<URRightsView>();
                        break;
                }
            }
        }
        catch (Exception ex)
        {
            URErrors.HandleError(ex, UserName);
            JavaScriptSerializer serial = new JavaScriptSerializer();
            string error = "URRights.Search Method: Could not Search Text - " + SearchText + ", SearchType = " + SearchType.ToString() + ",Id = " + Id.ToString() + Environment.NewLine;
            URErrors.HandleError(ex, UserName);
            throw new Exception(error);
        }
        return dRights;
    }

EXTRPATE更新

消息

Specified cast is not valid.

at System.Data.SqlClient.SqlBuffer.get_Int64()
at System.Data.SqlClient.SqlDataReader.GetInt64(Int32 i)
at Read_URRightsView(ObjectMaterializer`1 )
at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at URCore.URSecurity.URRights.Search(String SearchText, Int64 Id,  RightSearchType SearchType) in    D:\Development\Websites\unravelingspirit\URCore\URSecuirty\URRights.cs:line 244

0 个答案:

没有答案