How to select multiple fields (LINQ) - c#

How to change the following linq query to select another field value Field<int>("data_entry"),i want to select multiple fields .
var a = DF_Utilities.GetAvailableTasks(empnum, 1).AsEnumerable().Where(
p => p.Field<int>("task_code") == int.Parse(drpTasks.SelectedValue)).Select(p => p.Field<int>("cand_num")).First();
p.Field<int>("cand_num"),Field<int>("data_entry")
instead of p.Field<int>("cand_num")

You can use anonymous type:
var a = DF_Utilities.
GetAvailableTasks(empnum, 1).
AsEnumerable().
Where(p => p.Field<int>("task_code") == int.Parse(drpTasks.SelectedValue)).
Select(p => new
{
candNum = p.Field<int>("cand_num"),
dataEntry = p.Field<int>("data_entry")
}).
First();

Related

How to make group by inside group by in LINQ to Entity?

I have this LINQ to Entiy :
var grouppedClientFullReview = (from cfr in clientFullReview
group cfr by new { cfr.clientId, cfr.clientName } into g
select new
{
siteNumbers = g.Select(x => x.siteId).Count(),
clientId = g.Key.clientId,
clientName = g.Key.clientName,
WorkPlanReview = new
{
frequency = g.Select(x => x.inspectionFrequency),
isNormal = g.Any(x => x.isNormal == false) ? false : true,
objectsID = g.Select(x => x.objectID).ToArray(),
}
}).ToList();
I need to implement group by in WorkPlanReview property.
My question is how can I implement group by inside WorkPlanReview?
Is it posible to make nested group by?
If you want to group by the WorkPlanReview property itself, then you can end your query in the following way:
}).GroupBy(g => g.WorkPlanReview).ToList();
If you want to group on one of WorkPlanReview's properties, here's how to achieve it:
}).GroupBy(g => g.WorkPlanReview.frequency).ToList();

Select by ID from a list

I have a table of products and a table of categories, I can select by the ID of the Category like this:
var result = db.tblProducts.Where(p => p.tblCategories.Any(c => c.ID == 1));
However, I want to be able to select based on a list of Categories:
var catIDs = new List<int>() { 1,2,3 };
var results = db.tblProducts.Where(r => r.tblCategories.Any(t => catIDs.Contains(t.ID)));
I get the following error:
LINQ to Entities does not recognize the method 'Boolean Contains(Int32)' method, and this method cannot be translated into a store expression.
Presumably because I am using Contains to compare entities to local variables. Is there a way to do this?
Try create Expression from values. F.e.:
static Expression MakeOrExpression<T, P>(Expression<Func<T, P>> whatToCompare, IEnumerable<P> values)
{
Expression result = Expression.Constant(true);
foreach (var value in values)
{
var comparison = Expression.Equal(whatToCompare, Expression.Constant(value));
result = Expression.Or(result, comparison);
}
return result;
}
How to use:
var results = db.tblProducts.Where(r => r.tblCategories.Any(MakeOrExpression(t => t.ID, catIDs)));
The method MakeOrExpression will create an expression t.ID == 1 || t.ID == 2 || t.ID == 3 for list { 1, 2, 3 } dynamically, and then EF will translate it to SQL condition.
Maybe you can use this:
var catIDs = new List<int>() { 1,2,3 };
var results = db.tblCategories
.Where(t => catIDs.Contains(t.ID))
.SelectMany(t => t.tblProducts)
.Distinct();
Try this:
var query=from p in db.tblProducts
from c in p.tblCategories
where catIDs.Contains(c.ID)
select p;
If at least one of the categories of the product is in the catIDs list, then the product will be seleted.
Another option could be start by the categories (I'm guessing you have a many to many relationship between Product and Category and you have a collections of products in your Category entity):
var query=db.tblCategories.Where(c => catIDs.Contains(c.ID)).SelectMany(c=>c.tblProducts).Distinct();
Try this code :
var catIDs = new List<int>() { 1,2,3 };
var results = db.tblProducts.Where(r => catIDs.Any(c => c == r.tblCategories.Id));

How to select multiple fields from a DataView and apply .Distinct() using LINQ

I want to select some fields from DataView and after selecting those fields want to apply .Distinct() on these set of fields.
Right now, I used this code :
DataView dvGroups = new DataView();
dvGroups = GetDataFromDatabase(); //-- Fill Dataview
var groups = dvGroups.Table.AsEnumerable()
.Where(x => x.Field<int>("GroupId") != 0)
.Select(p => p.Field<int>("GroupId"))
.Distinct()
.ToArray();
it's only selecting a single field (i.e "GroupId"). But, Now i want to select multiple fields (like "GroupId", "GroupName") and then get the distinct value.
How can i achieve this task?
You can create anonymous objects:
.Select(p => new {
GroupId = p.Field<int>("GroupId"),
Something = p.Field<string>("Something"),
})
.Distinct().ToArray();
for example, because anonymous types are "compatible" with Distinct() (see LINQ Select Distinct with Anonymous Types), because the compiler generates the Equals/GetHashCode methods.
Or you could use the Tuple:
.Select(p => Tuple.Create(p.Field<int>("GroupId"), p.Field<string>("Something")))
But they are normally less clear.
More complex is to create your class and implement the Equals and GetHashCode
You can use an anonymous type:
var groups = dvGroups.Table
.AsEnumerable()
.Where(x => x.Field<int>("GroupId") != 0)
.Select(p => new
{
id = p.Field<int> ("GroupId"),
name = p.Field<string> ("Name"))
}).Distinct().ToArray();

Join an array of string with the result of an existing linq statement

As a follow up to my last question here:
Filtering a list of HtmlElements based on a list of partial ids
I need to take this statement:
doc.All.Cast<HtmlElement>()
.Where(x => x.Id != null)
.Where(x => ids
.Any(id => x.Id.Contains(id))).ToList();
and join it with an array of strings called fields. Assuming the array and list will have the same amount of elements each and line up correctly. I tried using Zip() but thought I might need to use an additional linq statement to make it work.
Assuming that fieldList[0] and IdList[0] corresponding to each other, you can do the following:
var IdList = doc.All.Cast<HtmlElement>()
.Where(x => x.Id != null)
.Where(x => ids
.Any(id => x.Id.Contains(id))).ToList();
var resultList = fieldList
.Select( (item, index) => new { Field = item, Id = IdList[index] })
.ToDictionary(x => x.Id, x => x.Field);
You have mentioned it already, you can use Enumerable.Join:
var joined = from id in fields
join ele in elements on id equals ele.Id
select new { Element = ele, ID = id };
var dict = joined.ToDictionary(x => x.ID, x => x.Element);
I've presumed that you want to join them via ID. I've also presumed that the string[] contains only unique ID's. Otherwise you need to use Distinct.

QueryOver: Dynamically adding IsLike in Where clause

I've this QueryOver where I select Log records where the Logs Name starts with D or F (using wildcards).
conv.InnerTransaction.Session.QueryOver<Log>()
.Where(l => l.DateTime > _datetime)
.And(
l => l.Name.IsLike("D%") || l.Name.IsLike("F%")
)
Instead I would like the name searching to be dynamically using values from a list. How can this be done?
I've tried something like:
var query = conv.InnerTransaction.Session.QueryOver<Log>()
.Where(l => l.DateTime > _datetime);
foreach (var name in _names)
{
query = query.And(l => l.Name.IsLike(name));
}
But that would result in multiple AND statements for each name in the list, whereas It just need to be a OR.
Have you tried Disjunction? I had a similar requirement once, but I had to use Conjunction instead. Disjunction will or multiple conditions together.
var disjunction = new Disjunction();
var query = Session.QueryOver<Log>().Where(l => l.DateTime > _datetime);
foreach (var name in _names)
{
disjunction.Add(Restrictions.On<Log>(log => log.Name).IsLike(name));
}
var queryResult = query.Where(disjunction).List<Log>();

Categories