I have a dropdown list where it is populated from the database I want to remove an a number from the options list. This is the code
var id= ctx.tableMaster.Select(x => x.id).Where(x != "0").Distinct();
What would I put in the were statement to check if the idis not equal to 0
Thanks
Is id an integer? If so, you don't need quotes:
var idList = ctx.tableMaster.Where(x => x.id != 0).Select(x => x.id).Distinct();
You could try this too
var results = ctx.tableMaster.FindAll(x => !x.id.Equals(0));
If you have to ignore multiple ids
List<int> ignoreIds = new List<int>() { 0, 1, 5, 9};
var results = ctx.tableMaster.FindAll(x => !ignoreIds.Contains(x.id));
Related
This is what i am trying to achieve:
var result = (string)null;
foreach (var tagId in MultipleTags)
{
result = string.Join(",", db.Tags.Where(t => t.TagId == tid).Select(e => e.TagName));
}
This result variable should hold all the values in comma seperated form while MultipleTags contain sets of ids so it will iterate more than one time.
how should i write it. please suggest me.
I am doubtfull if this line is correctly written.
db.Tags.Where(t => t.TagId == tid).Select(e => e.TagName)
please suggest me something.
This does the trick:
//assuming that MultipleTags is an array of "ids"
var result = db.Tags.Where(t => MultipleTags.Contains(t.TagId)).Select(e => e.TagName).ToList().Aggregate((a, b) => a + "," + b);
Try this too
var result = db.Tags.Where(t => MultipleTags.Contains(t.TagId)).ToList().ForEach((item) =>
{
item.TagName = item.id + "," + item.TagName;
});
try using this
var result = (string)null;
result=string.Join(",", (from a in db.Tags join m in MultipleTags on a.TagId equals m select a.TagName));
I use this little piece of code to get the IDs I need, and put them in an array
var userids = from a in db.Person
select a.idlist;
string[] idarray = userids.FirstOrDefault().ToString().Split(';');
How can I use this array of ids to select the matching rows in another query like this
[HttpGet]
public ActionResult GetStuff()
{
var Item = from a in db.Table
where a.id == idarray[0]
and where a.id == idarray[1]
and where a.id == idarray[2]
etc...
select new
{
a.itemid,
a.Element
};
return Json(Item, JsonRequestBehavior.AllowGet);
}
Try something like this:
var Item = from a in db.Table
where idarray.Contains(a.id)
select new
{
a.itemid,
a.Element
};
var Item = from a in db.Table
where idarray.Contains(a.id)
select new
{
a.itemid,
a.Element
}.ToArray();
Don't you want to use Extension Method Lambda syntax? I is same Linq, but just has more code-like view:
var Item = db.Table.Where(x => x.Contains(a.id))
.Select(x => new
{
a.itemid,
a.Element
}).ToArray();
Here's what I usually do.
Get the IDs you need:
var ids = something.Where(s => s.SomeCondition).Select(s => s.Id);
Now Lets get the data based on the Ids:
var response = anothertable.Where(a => ids.Contains(a.Id);
You then can make it a list, array, or whatever you want to do with it. It will go through all the records in anothertable and find the records where the a.Id matches any of the ids.
I have the following DB structure.
I want to make a select using Entity Framework. I want all my Categories that MAY have a CategoryText. If They have a CategoryText I also need to get the Language.
I searched and I couldn't find anything useful.
Here is my query that doesn't work:
var categoriesSQL = db.Categories
.Include(i => i.CategoryTexts.Select(s => s.Language)
.Where(w => w.Format == (string)Session["chosen_language"]));
var categories = categoriesSQL.ToList();
It throws:
The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.
Parameter name: path
I tried to solve this in a lot of ways and recomandations but I couldn't find a solution.
I want the select made in only one query.
Thank you!
Try this
var language = (string)Session["chosen_language"];
var categoriesSQL = db.Categories
.Include(i => i.CategoryTexts.Select(s => s.Language))
.Where(c =>
(from ct in c.CategoryTexts
from l in ct.Languages
select l.Format).Contains(language)
);
var categories = categoriesSQL.ToList();
OR
var language = (string)Session["chosen_language"];
var categoriesSQL = db.Categories
.Include(i => i.CategoryTexts.Select(s => s.Language))
.Where(c => c.CategoryText
.Any(ct => ct.Languages
.Any(l => l.Format == language)
)
);
var categories = categoriesSQL.ToList();
I have this in my method:
var qry = db.Forms.Take(4)
.Where(m => m.SateliteID == Id)
.OrderByDescending(m => m.Tanggal)
.ToArray();
What I want is getting the last 4 records from all the records available, but
what I get is the first 4 records.
What I have done wrong?
I thought that command will be the same with this:
SELECT TOP 4 <fields> FROM Forms WHERE sateliteID = Id
ORDER BY tanggal DESC
But it seems they are a different.
What should I do to get what I want (the last 4 records instead of the first 4 records)?
Thanks in advance for the help.
Move your Take:
var qry = db.Forms.Where(m => m.SateliteID == Id)
.OrderByDescending(m => m.Tanggal)
.Take(4)
.ToArray();
var qry = db.Forms.Where(m => m.SateliteID == Id).OrderByDescending(m => m.Tanggal).Take(4).ToArray();
You should order and than take some results from ordered list. Methods should be called in that kind of order.
JUST get apply you where and orderby clause first and than apply take function that will do the work for you
var qry = db.Forms
.Where(m => m.SateliteID == Id)
.OrderByDescending(m => m.Tanggal)
.Take(4)
.ToArray();
For future use :LINQER
this will allow you to convert you SQL QUERIES TO LINQ easily.....very helpful tool
Using linqtosql, how would I get a colleciton of User objects, if I have an array of UserID's that I want to fetch?
You can use Contains to check if each UserID is in the array that you have.
int[] userIDs = ...
var users = db.Users.Where( u => userIDs.Contains( u.UserID ) );
If you want to avoid a SET operation and user chained ORs instead, you can use the PredicateBuilder to help you with that.
It goes something like this:
var userIDs = new[] { 1, 2, 3, 4, 5 };
// build multiple OR expressions
var filter = PredicateBuilder.False<User>();
foreach (var id in userIDs) {
filter = filter.Or(x => x.UserID == id);
}
// fetch data
using (var db = new TheDataContext()) {
var users = db.Users.Where(filter);
// wham! - we have users now.
}
Take a look at the blog post to understand how it works. This basically creates a long chaining ORs for each user id in the list before passing it to a WHERE clauses.
Try this:
using(var db=new MyDataContext())
{
var users=db.Users.Where(u=>userIds.Contains(u.Id));
}