I have created a variable that returns a list ids of menus from the database. I have then put the variable in another query that returns meals in a list. My issue is that I keep on recieving the error Cannot implicitly convert type System.Collections.Generic.List<int?> to int?
var getmenuids = (from r in db.Menus
where (r.Restaurantid == currentrestaurant)
select r.Id).ToList();
var meals = db.Meals.Where(r => r.MenuID = getmenuids).ToList();
You're having an issue because you're using == (comparing a single element to a list) instead of Contains() (searching for the an element in the list).
var meals = db.Meals.Where(r => getmenuids.Contains(r.MenuID)).ToList();
You could combine these too.. something like this:
var meals = (from meal in db.Meals
join menu in db.Menus on meal.MenuID equals menu.Id
where menu.Restaurantid == currentrestaurant
select meal).ToList();
The problem here is that getmenuids is a collection of int? values but you compare it to r.MenuID which is typed to int?. To fix this you need to search getmenuids for r.MenuId
db.Meals.Where(r => getmenuids.Contains(r.MenuID)).ToList();
You are comparing the list with MenuID. getmenuids is a list.
r.MenuID = getmenuids
Related
How would you write "SELECT col.a, col.b FROM TABLE WHERE ID = 1" in LINQ to Entity 6 so that you could save col.a into variable A and col.b into variable B. You can do this with SqlReader by retrieving the index or column name, but with LINQ to Entity it is returned as one object. When returning a two field LINQ query to a list, both field are saved to the first index of the list and can only be accessed as the same list element. Is it standard to create two LINQ queries, one for each variable? Or am I missing part of the process?
Current query:
var result = (from col in cxt.Table
where col.ID == "1"
select new {col.a, col.b}).ToList();
If you are expecting exactly one record to return from database then what you can do:
var result = cxt.Table.FirstOrDefault(x => x.ID == "1");
//Notice that might be null if has not result
var a = result.a;
var b = result.b;
Will look very similar in query syntax but I think this method syntax neater in this case
I am not sure what exactly you are looking for. But selecting two variable by Linq is not so hard.
var value = (from ta in db.YourTable
where ta.ID = id
select new {
A = ta.a,
B = ta.b
}).FirstOrDefault();
var a = value.A;
var b = value.B;
If you use ToList() instead of FirstOrDefault(), you will get a list contain zero or more objects. You can simply use a loop to get field from each object.
forach(var value in values)
{
var a = value.A;
var b = value.B;
}
I have 2 list of items;
IEnumerable<Investigator> investigators = RepositoryContext.InvestigatorRep.GetInvestigators(site.Site.Id, out totalCount);
var NewInvestigators = base.ActivePage.Investigators;
I have 30 items in investigators and 20 items in NewInvesigators, both have property Id, InvId I need to match that.
var all = investigators.Where(b => crInvestigators.Any(a => a.InvestigatorId == b.Id));
I tried this but not worked
I want to create a new list based on matching Id of those two lists. If Id matches get the particular investigator(basically a sort on investigators based on Id existing in NewInvesigators).
I can do it by using for each, but I want to know whether it is possible with linq?
in newinvestigator I have object which is having two property, investigatorId and Name.
In Investigator I have property Id , City , country.
no Name or investigatorId in the investigator list.
You could try something like this:
var result = investigators.Where(inv=>NewInvestigators.Any(ninv=>ninv.id == inv.investigatorId))
.OrderBy(inv=>inv.id);
Another way to get the same result is using a join.
var result = from inv in investigators
join ninv in NewInvestigators
on inv.id equals ninv.investigatorId
order by inv.id
select inv;
I have list e.g:
list1=
{{id=1,address=a},
{id=1,address=b},
{id=2,address=c},
{id=2,address=d}}
how can i change this list into something like
list2=
{{id=1,address={a,b}},
{id=2,address={c,d}}}
that is putting same id's list into one with inner list containing other elements
group is your friend here. Assuming the output type's address property is a List<T> something like:
var res = (from x in input
group x by x.id into grouped
select new Output {
id = grouped.Key,
address = grouped.ToList()
}).ToList();
I have struggled with it for a long time. I have two collections: MyRepository.All and MyCollection, both holds the collection of objects which has ID property. I need to get result of list of objects from MyRepository.All what contains only objects which id's are equal to MyCollection's objects'ids.
ICollection MyCollection // as parameter to method
var result = MyRepository.All.Where(r=>r.id==MyCollection.???.id).ToList();
i need to replace ??? with some linq to get this done.
ive tried different where and select caluses, excist and intersect and so on..
from a in MyRepository.All
join m in MyCollection on a.Id equals m.Id
select a
Cache the ids of MyCollection into a HashSet.
Than you can retrieve your result with a Where clause like this :
var myIdSets = new HashSet(MyCollection.Select(c => c.Id));
var result = MyRepository.All.Where(r=> myIdSets.Contains(r.id)).ToList();
var result = (from r in MyRepository.All
join r2 in MyCollection on r.id equals r2.id
select r).ToList();
MyRepository.All.Where(r=>MyCollection.Select(a=>a.id).Contains(r.id))
Linq has an .Intersect that should get you want you need.
Something like this:
var result = MyRepository.Intersect(MyCollection).ToList();
More info:
http://msdn.microsoft.com/en-us/library/system.linq.enumerable.intersect.aspx
I need to pull all values where the request type is any of the ones I have on the list.
from v in ctx.vEmailSents
where v.RequestType_ID == reqTypeID
group v by v.SentToLab_ID into g
select g.OrderByDescending(x => x.DateSent).FirstOrDefault() into lastV
select new
{
ClaimID = lastV.Claim_ID,
};
reqTypeID is of type List<int>.
How can I use it in Linq to get all records that are in that list?
You can do something like this:
where requestTypes.Contains(v.RequestType_ID)
requestTypes would be the list you talked about.