Select from list linq - c#

I am trying to get matches from linq query-
public ActionResult TagFilter(TagModel tag) {
List<CardModel> cardlist = null;
var cardtaglist = (from u in db.CardTagTables
where u.CardTagName == tag.tagName
select u).ToList();
cardlist = (from u in db.CardTables
where u.CardID == cardtaglist.Where(e=>e.FKCardTagID==u.CardID)
select new CardModel {
cardHashCode = tag.tagName,
cardDate = u.CardDate,
cardFileName = u.CardFileName,
cardFilePath = u.CardFilePath,
cardID = u.CardID,
cardTitle = u.CardTitle
}).ToList();
if (cardlist.Count == 0) {
return Json(new { success = false });
}
else {
return PartialView("_FunHomePartial", cardlist);
}
}
Where match of tag=>tagName would be from list cardtaglist.
I get Cannot implicitly convert type int to bool error in line-
where u.CardID == cardtaglist.Where(e=>e.FKCardTagID==u.CardID)
How Do I match elements from list cardtaglist ?

How to about replace
u.CardID == cardtaglist.Where(e=>e.FKCardTagID==u.CardID)
with
cardtaglist.Any(e=>e.FKCardTagID==u.CardID)

First of all, why you select all CardTagTable entity, if you use only FKCardTagID!? The best way - to select only required fields:
var cardtagIds = (from u in db.CardTagTables
where u.CardTagName == tag.tagName
select u.FKCardTagID).ToList();
About your error, you are traying to compare IQueriable with numeric value. You can use Contains method in this case:
cardlist = (from u in db.CardTables.Where(u => cardtagIds.Contains(u.CardID));
select new CardModel {
....
Edit
Also, this query can be optimized:
cardlist = (from u in db.CardTables.Where(u =>
db.CardTagTables
.Where(ct => ct.CardTagName == tag.tagName)
.Select(ct => ct.FKCardTagID)
.Contains(u.CardID))
select new CardModel {
....

Use:
var result=cardtaglist.Any(e=>e.FKCardTagID==u.CardID)

Related

Aggregate of string on anonymous type

I want to concat multiple string value into single string with comma separated,i tried using aggregate function but it shows cannot convert string to how to fix this issue,
I tried below code
var res = (from e in WYNKContext.SurgeryAssigned.Where(x => x.CmpID == cmpid && x.IsCancelled == false)
select new
{
ID = e.SAID,
UIN = e.UIN,
SurgeryDate = e.SurgeryDate,
SurgeryID = e.SurgeryID,
Surgery = ((from st in WYNKContext.SurgeryTran.
Where(x => x.SurgeryID == e.SurgeryID)
select new
{
desc = icdmaster
.Where(x => x.ID ==
st.IcdSpecialityCode).Select(x =>
x.SpecialityDescription).FirstOrDefault(),
}).ToList()).Aggregate((a, b) => a.desc + "," + b.desc),
}).ToList();
I want Output like inside surgery property like = string1,string 2 ,etc....
without using aggregate i am getting as count in Surgery Property
var res = (from e in WYNKContext.SurgeryAssigned.Where(x => x.CmpID == cmpid && x.IsCancelled == false)
select new
{
ID = e.SAID,
UIN = e.UIN,
SurgeryDate = e.SurgeryDate,
SurgeryID = e.SurgeryID,
Surgery = (from st in WYNKContext.SurgeryTran.Where(x => x.SurgeryID == e.SurgeryID)
select new
{
icd = icdmaster.Where(x => x.ID == st.IcdSpecialityCode).Select(x => x.SpecialityDescription).FirstOrDefault(),
}).ToList(),
}).ToList();
also tried string join :
Surgery = string.Join(",", (from st in WYNKContext.SurgeryTran.Where(x => x.SurgeryID == e.SurgeryID)
select new
{
icd = icdmaster.Where(x => x.ID == st.IcdSpecialityCode).Select(x => x.SpecialityDescription).FirstOrDefault(),
}).ToList()),
but in output i am getting like this
Surgery ={ icd = CORNEA },{ icd = CATARACT/IOL }
can some one tell what i did wrong in string.join.....
The string class has a static method named Join, which takes in a collection of items and a string to join them with, which should work for you here.
If I'm reading your code correctly, it would look something like this:
Surgery = string.Join(",", WYNKContext.SurgeryTran
.Where(surgTran => surgTran.SurgeryID == e.SurgeryID)
.Select(surgTran => icdmaster
.Where(icd => icd.ID == surgTran.IcdSpecialityCode)
.Select(icd => icd.SpecialityDescription)
.FirstOrDefault())),

Change Inner Select Value in Lambda Expression C#

I have a query in Lambda expression. i want to encode a properties which in two step inner in query.
The sample code is
public List<StudentCreativeQuestionListViewModel> GetCreativeQuestionsByQuestionSetId(long studentId,
long questionSetId)
{
var questionList =
UnitOfWork.StudentExamRepository.GetCreativeQuestionsByQuestionSetId(studentId, questionSetId);
List<StudentCreativeQuestionListViewModel> encryptQuestionList = questionList.Select(q =>
{
q.Questions = q.Questions.Select(t =>
{
t.TutorialList.Select(x =>
{
x.FileName = x.FileName.ToEncode();
return x;
});
return t;
});
return q;
}).ToList();
return encryptQuestionList.ToList();
}
and GetCreativeQuestionsByQuestionSetId exist in another layer having code:
public IEnumerable<StudentCreativeQuestionListViewModel> GetCreativeQuestionsByQuestionSetId(long studentId, long questionSetId)
{
bool isPurchased = this.IsPurchased(studentId);
var data = (from question in Context.Question
join questionSetQuestion in Context.QuestionSetQuestion on question.Id equals questionSetQuestion
.QuestionId
where questionSetQuestion.QuestionSetId == questionSetId && question.IsArchived == false
select new StudentCreativeQuestionListViewModel
{
Id = question.Id,
Name = question.Name,
Mark = questionSetQuestion.Mark,
LastUpdateDate = question.LastUpdateDate,
ImageUrl = question.ImageUrl,
Questions = Context.CreativeQuestion.Where(qa => qa.QuestionId == question.Id).AsEnumerable()
.Select(cq => new CreativeQuestionViewModel
{
Id = cq.Id,
Name = cq.Name,
TutorialList = (from aSuggestion in Context.AnswerSuggestion
join t in Context.Tutorial on aSuggestion.TutorialId equals t.Id
where aSuggestion.CreativeQuestionId == cq.Id &&
t.TutorialType >= TutorialType.Video &&
t.TutorialType <= TutorialType.Link
group t by t.TutorialType into grp
select grp.OrderBy(g => g.TutorialType).FirstOrDefault() into tutorial
join st in Context.StudentTutorial.Where(s => s.StudentId == studentId) on tutorial.Id equals st.TutorialId into sTutorialTemp
from sTutorial in sTutorialTemp.DefaultIfEmpty()
join topic in Context.Topic on tutorial.TopicId equals topic.Id into topicGroup
from tp in topicGroup.DefaultIfEmpty()
join chapter in Context.Chapter on tutorial.ChapterId equals chapter.Id into chapterGrp
from c in chapterGrp.DefaultIfEmpty()
join bk in Context.Bookmark on tutorial.Id equals bk.TutorialId into tempBk
from bookmark in tempBk.Where(t => t.StudentId == studentId).DefaultIfEmpty()
select new TutorialListViewModel
{
Id = tutorial.Id,
Body = tutorial.Body,
Heading = tutorial.Heading,
FileName = tutorial.FileName,
ThumbUrl = tutorial.ThumbUrl,
ChapterName = c.Name,
TutorialType = tutorial.TutorialType,
DurationInSecond = tutorial.DurationInSecond,
TopicId = tutorial.TopicId,
Sequence = tp != null ? tp.SequenceNumber:0,
IsLocked = tutorial.IsLocked && !isPurchased,
IsCompleted = sTutorial != null && sTutorial.IsCompleted,
IsBookmark = bookmark != null
}).ToList()
}).ToList()
}).AsEnumerable();
return data;
}
Here show the error to cast this query. the message shown:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?)
I think what you are doing with q.Questions = q.Questions.Select(t =>... while creating the encryptedQuestionList is causing the cast exception as the 'q.Questions' is of type 'List' and you are trying to assign an 'IEnumerable' to it within that query.
The encryptedQuestionList query can be written like this:
List<StudentCreativeQuestionListViewModel> encryptQuestionList = questionList.Select(q =>
{
q.Questions.Select(t =>
{
t.TutorialList.Select(x =>
{
x.FileName = x.FileName.ToEncode();
return x;
});
return t;
});
return q;
}).ToList();
List<StudentCreativeQuestionListViewModel> encryptQuestionList = questionList.Select(q =>
{
q.Questions = q.Questions.Select(t =>
{
t.TutorialList = t.TutorialList.Select(x =>
{
x.FileName = x.FileName.ToEncode();
return x;
}).ToList();
return t;
}).ToList();
return q;
}).ToList();

Linq Subquery in IQueryable Select

I am having problems with subqueries in a linq-to-entities IQueryable query.
Can anybody tell me what is the correct way to do a sub query when building up an IQueryable query? Based on my full example below.
I get the following error when ToArray() is called on the query:
An exception of type 'System.NotSupportedException' occurred in System.Data.Entity.dll but was not handled in user code
Additional information: LINQ to Entities does not recognize the method 'System.Data.Entity.IDbSet`1[MyDomain.NewsWorthyPressReleases.NewsWorthyWire] Set[NewsWorthyWire]()' method,
and this method cannot be translated into a store expression.
So for example you can see one of my subqueries here:
BwIncluded = (from abw in Context.Set<NewsWorthyWire>()
where abw.WireId == PressWireIds.BUID
where abw.NewsWorthyCampaignId == nc.Id
select abw).Any(),
I am trying to populate BwIncluded with true or false if any() exists.
And the entire build up of the query is as follows, it is IMPORTANT to understand where the sub-queries are used:
private Func<EntityFramework.IDbContext, IQueryable<NewsWorthyPressRelease>> GetSearchNewsWorthyQuery(NewsWorthySearchFields searchFields)
{
return context =>
{
//domain models
var newsWorthyCampaigns = context.Set<NewsWorthyCampaign>();
var wires = context.Set<NewsWorthyWire>();
var campaigns = context.Set<Campaign>();
//predicates
var newsWorthyCampaignPredicate = PredicateBuilder.True<NewsWorthyCampaign>();
var wirePredicate = PredicateBuilder.True<NewsWorthyWire>();
var campaignPredicate = PredicateBuilder.True<Campaign>();
// build up the predicate queries
//Status was input
if (searchFields.StatusId.HasValue && searchFields.StatusId.Value > 0)
{
newsWorthyCampaignPredicate = newsWorthyCampaignPredicate.And(x => x.Id == searchFields.StatusId);
}
//Id was input
if (searchFields.Id.HasValue && searchFields.Id.Value > 0)
{
wirePredicate = wirePredicate.And(x => x.Id == searchFields.Id);
}
//Title was input
if (!string.IsNullOrEmpty(searchFields.Title))
{
wirePredicate = wirePredicate.And(x => x.Title.Contains(searchFields.Title));
}
//Wire was input
if (searchFields.PressWireId.HasValue && searchFields.PressWireId.Value > 0)
{
wirePredicate = wirePredicate.And(x => x.Id == searchFields.PressWireId);
}
//Created By was chosen
if (searchFields.CreatedById.HasValue && searchFields.CreatedById.Value > 0)
{
campaignPredicate = campaignPredicate.And(x => x.IdentityId == searchFields.CreatedById);
}
//Create Date From was chosen
if (searchFields.DateCreatedFrom.HasValue)
{
campaignPredicate = campaignPredicate.And(y => y.CreationDate >= searchFields.DateCreatedFrom);
}
//Create Date To was chosen
if (searchFields.DateCreatedTo.HasValue)
{
campaignPredicate = campaignPredicate.And(y => y.CreationDate <= searchFields.DateCreatedTo);
}
//the query
var nwprQuery = (from nc in newsWorthyCampaigns.Where(newsWorthyCampaignPredicate)
join w in wires.Where(wirePredicate)
on nc.Id equals w.NewsWorthyCampaignId
join c in campaigns.Where(campaignPredicate)
on nc.CampaignId equals c.Id
select new NewsWorthyPressRelease
{
Id = nc.Id,
Title = w.Title,
//BwIncluded = false,
//GnIncluded = false,
//PrIncluded = false,
BwIncluded = (from abw in Context.Set<NewsWorthyWire>()
where abw.WireId == PressWireIds.BUID
where abw.NewsWorthyCampaignId == nc.Id
select abw).Any(),
GnIncluded = (from agw in Context.Set<NewsWorthyWire>()
where agw.WireId == PressWireIds.GLID
where agw.NewsWorthyCampaignId == nc.Id
select agw).Any(),
PrIncluded = (from anw in Context.Set<NewsWorthyWire>()
where anw.WireId == PressWireIds.PRID
where anw.NewsWorthyCampaignId == nc.Id
select anw).Any(),
CreatedBy = c.CreatedBy.FirstName + " " + c.CreatedBy.Surname,
CreateDate = c.CreationDate,
Status = nc.NewsWorthyStatus.Name,
}).AsNoTracking();
return nwprQuery;
};
}
Can anyone help with this?
Thanks

Create a search method with AND/OR options in Linq and C#

I'm trying to create some methode for searching and filtring data in databese using c# and asp.net mvc 4 (linq)
public ActionResult Search_Names_Using_Location(string b,string d, int c=0,int Id=0)
{
ViewBag.Locations = db.Locations.ToList();
var agentlocation = new AgentLocationViewModel();
agentlocation.agents = new List<Agent>();
agentlocation.agents = (from a in db.Agents
where a.LocationId == Id
&& (a.LocationName == b)
&& (a.age > c )
select a).ToList();
return View(agentlocation);
}
The problem is that user can let some texboxes empty, so the value of Id or a or b can be null so the query will get nothing.
Is their any suggestions to do that (i can go with if else but that's hard if i have 7 or 8 strings)?
You can check for null inside query
public ActionResult Search_Names_Using_Location(string b,string d,
int c=0,int Id=0,)
{
ViewBag.Locations = db.Locations.ToList();
var agentlocation = new AgentLocationViewModel();
agentlocation.agents = new List<Agent>();
var noId = string.IsNullOrWhitespace(Id);
var noB = string.IsNullOrWhitespace(b);
agentlocation.agents = (from a in db.Agents
where (noId || a.LocationId == Id)
&& (noB || a.LocationName == b)
&& (a.age > c )
select a).ToList();
return View(agentlocation);
}
If you have AND conditions only you can use
var query = db.Agents;
if (Id != 0)
{
query = query.Where(x => x.LocationId == Id)
}
if (!string.IsNullOrWhitespace(b))
{
query = query.Where(x => x.LocationName == b)
}
...
var result = query.ToList(); // actual DB call
This will remove useless empty conditions, like WHERE (0 = 0 OR LocationId = 0)
In case of OR conditions and combinations you can take a look at PredicateBuilder
So you can use Or and And predicate combinations like this:
IQueryable<Product> SearchProducts (params string[] keywords)
{
var predicate = PredicateBuilder.False<Product>();
foreach (string keyword in keywords)
{
string temp = keyword;
predicate = predicate.Or (p => p.Description.Contains (temp));
}
return dataContext.Products.Where (predicate);
}

dynamic join based on where expression - linq/c#

I have a sp which builds a dynamic sql query based on my input params. I tried replicating in linq and somehow it seems incorrect.
My linq:
var result = from R in db.Committees.Where(committeeWhere)
join C in db.Employees.Where(employeeWhere) on R.PID equals C.PID
join K in db.CommitteeTypes.Where(committeesWhere) on R.PID equals K.PID
select new { R };
The 3 input params i have are:
1. Committee ID and/or
Employee ID and/or
Committee Type ID
Based on this, i want to be able to make the joins in my linq.
Note: i had to change table names and column names so please do not give thought on the names.
Sql snippet:
IF #committeeID is not null
set #wherestr = #wherestr + 'Committees.committeeID like' + #committeeID + #andstr
//...
IF len(#wherestr) > 6
SELECT #qrystr = #selectstr + #fromstr + left(#wherestr, len(#wherestr)-3) + ' ORDER BY Committees.committeeID DESC
EXEC (#qrystr)
Why do you need to use dynamic SQL? Wouldn't this work?
IQueryable<Committee> GetCommittees(int? committeeID, int? employeeID, int? committeeTypeID)
{
var result = from R in db.Committees.Where(c => committeeID == null || committeeID == c.ID)
join C in db.Employees.Where(e => employeedID == null || employeeID == e.ID)
on R.PID equals C.PID
join K in db.CommitteeTypes.Where(c => committeeTypeID == null || committeeTypeID == c.ID)
on R.PID equals K.PID
select R;
}
If that won't work, you can use different predicate expressions depending on your parameters:
Expression<Func<Committee, bool>> committeeWhere;
if(committeeID.HasValue)
{
int id = committeeID.Value;
committeeWhere = c => c.ID == id;
}
else
{
committeeWhere = c => true;
}
// etc
Update: Seeing your last comment, maybe you want something like this:
IQueryable<Committee> GetCommittees(int? committeeID, int? employeeID, int? committeeTypeID)
{
var result = db.Committees.Select(c => c);
if(committeeID.HasValue)
{
result = result.Where(c => c.ID = committeeID);
}
else if(employeeID.HasValue)
{
result = from R in result
join C in db.Employees.Where(e => employeeID == e.ID)
on R.PID equals C.PID
select R;
}
else if(committeeTypeID.HasValue)
{
result = from R in result
join K in db.CommitteeTypes.Where(ct => committeeTypeID == ct.ID)
on R.PID equals K.PID
select R;
}
return result;
}
If I may improve upon dahlbyk's answer... sometimes joining introduces duplicates. If you really intend to filter - then filter. Also - if you add the relationships in the LinqToSql designer, you'll have properties (such as Committee.Employees) which will be translated for you.
IQueryable<Committee> GetCommittees(int? committeeID, int? employeeID, int? committeeTypeID){
IQueryable<Committee> result = db.Committees.AsQueryable();
if(committeeID.HasValue)
{
result = result.Where(c => c.ID = committeeID);
}
if(employeeID.HasValue)
{
result = result
.Where(committee => committee.Employees
.Any(e => employeeID == e.ID)
);
}
if(committeeTypeID.HasValue)
{
result = result
.Where(committee => committee.CommitteeTypes
.Any(ct => committeeTypeID == ct.ID)
);
}
return result;
}

Categories

Resources