在c#linq中,我有这段有效的代码
Event event = mViewModel.findSpecificEvent(1);
// An example to pass it to recyclerVeiw
recyclerViewAdapter.setEvent(event);
我有这个
private IQueryable<v_CompanyInquiryInfo> SearchCompany(string pCompanyName)
{
var mCompany = from d in db.v_CompanyInquiryInfo
where d.CompanyName.ToLower().Equals(pCompanyName)
select ((v_CompanyInquiryInfo)d);
return mCompany;
}
也可以。但是,我希望第二个函数的返回类型为 private IQueryable SearchCompanies(string pValues)
{
string mValues = pValues;
foreach (string lWord in iRestrictedWords)
{
mValues.Replace(lWord, "");
}
var mSearchArray = pValues.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var mCompanyCriteria = db.v_CompanyInquiryInfo.AsExpandable().Where(TradingInquiryController.CompanyContainsSearchTerms(mSearchArray));
var mCompanies = mCompanyCriteria
.Select(x => new
{
x.CompanyName,
x.CompanyID,
x.SearchTags,
Rank = mSearchArray.Sum(s => ((x.SearchTags.Length - x.SearchTags.Replace(s, "").Length) / s.Length))
});
var mResults = mCompanies.OrderByDescending(o => o.Rank).Skip(0).Take(20);
return mResults;
}
。问题是添加了IQueryable<v_CompanyInquiryInfo>
个新的动态列,然后按它排序。如何应用相同的顺序但不为其创建新列,然后将强制类型转换应用于Rank
。这样我就可以返回v_CompanyInquiryInfo
。我无法弄清楚它的语法。它还可以返回所有列。
谢谢
答案 0 :(得分:0)
您无需创建该属性:
private IQueryable SearchCompanies(string pValues)
{
string mValues = pValues;
foreach (string lWord in iRestrictedWords)
{
mValues.Replace(lWord, "");
}
var mSearchArray = pValues.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var mCompanyCriteria = db.v_CompanyInquiryInfo.AsExpandable().Where(TradingInquiryController.CompanyContainsSearchTerms(mSearchArray));
var mCompanies = mCompanyCriteria
.OrderByDescending(x => mSearchArray.Sum(s => ((x.SearchTags.Length - x.SearchTags.Replace(s, "").Length) / s.Length))
;
//var mResults = mCompanies.Skip(0).Take(20);
return mCompanies;
}