LINQ Select distinct on different variables - c#

I have a query which selects a list of my class. It looks like so:
IQueryable<ClaimsBySupplierAggregate> agg =
(from d in alliance.SupplierSearchByReviewPeriod
where d.ClientID == ClientID && ReviewPeriodIDs.Contains((int)d.ReviewPeriodID)
select new ClaimsBySupplierAggregate {
Amount = d.Amount,
StatusCategoryID = d.StatusCategoryID,
DeptName = d.DepartmentName,
APLReason = d.APLReason,
Area = d.AreaDesc,
StatusCategoryDesc = d.StatusCategoryDesc,
Agreed = d.Agreed
});
Later on in the application I select each variable and get the distinct values like this:
SupplierModel.APLReason = agg.Select(r => r.APLReason).Distinct().ToList();
SupplierModel.AreaDesc = agg.Select(r => r.Area).Distinct().ToList();
SupplierModel.DeptName = agg.Select(r => r.DeptName).Distinct().ToList();
SupplierModel.StatCatDes = aggg.Select(r => r.StatusCategoryDesc).Distinct().ToList();
Is there a way to do this in one LINQ statement?

You could using Aggregate, but you would need a complex object for the seed, which brings you to the same complexity of code. I think that for this particular case, using LInQ enters the Golden Hammer antipattern. Just use an old fashioned loop and four HashSets instead of Lists and you are done and the code is more readable and your intention clearer.

I think you can use this then customize the code follow your expectation.
Or you can use group by.
public class LinqTest
{
public int id { get; set; }
public string value { get; set; }
public override bool Equals(object obj)
{
LinqTest obj2 = obj as LinqTest;
if (obj2 == null) return false;
return id == obj2.id;
}
public override int GetHashCode()
{
return id;
}
}
List<LinqTest> uniqueIDs = myList.Distinct().ToList();

There is a contrived way to do this:
from d in alliance.SupplierSearchByReviewPeriod
where d.ClientID == ClientID && ReviewPeriodIDs.Contains((int)d.ReviewPeriodID)
group d by 0 into g
select new
{
APLReasons = g.Select(d => d.APLReason).Distinct(),
AreaDescs = g.Select(d => d.Area).Distinct(),
DeptNames = g.Select(d => d.DeptName).Distinct(),
StatusCategoryDescs = g.Select(d => d.StatusCategoryDesc).Distinct(),
}
The part group d by 0 into g creates one group of all items, from which you can subsequently query any aggregate you want.
BUT...
...this creates a very inefficient query with UNIONs and (of course) DISTINCTs. It's probably better (performance-wise) to simply get the flat data from the database and do the aggregations in subsequent code.

Related

LINQ query in C#?

I'm pretty new to C# and Linq programming world.
I Want to do something similar to this, but SubType is a FK to table Type and I can't do what I've done in this example:
public static List<DropdownModel> GetSubTypes(List<string> ListTypes)
{
List<DropdownModel> SubTypes = new List<DropdownModel>();
using (DocumentXtractorEntities DataBase = new DocumentXtractorEntities())
{
foreach (string TypeID in ListTypes)
{
int TypeIDINT = Int32.Parse(TypeID);
SubTypes.AddRange((from C in DataBase.SubType.Where(s => s.Active && s.TypeID == TypeIDINT)
select new DropdownModel()
{
ID = C.SubTypeID,
Description = C.Name,
Selected = false
}).ToList());
}
}
return SubTypes;
}
So, the code above kinda filters the subtype text box when I chosen one or more Types.
Now, I need to do the opposite, fill the Type List when subtypes are chosen.
I've tried something but I know that isn't possible the way I'm doing this.
My code for now is this:
public static List<DropdownModel> GetTypesBySubTypes(List<string> ListSubTypes)
{
List<DropdownModel> Types = new List<DropdownModel>();
using (DocumentXtractorEntities DataBase = new DocumentXtractorEntities())
{
foreach (string SubTypeID in ListSubTypes)
{
int SubTypeIDINT = Int32.Parse(SubTypeID);
Types.AddRange((from C in DataBase.Type.Where(s => s.Active && s.SubType.Contains(SubTypeIDINT))
select new DropdownModel()
{
ID = C.TypeID,
Description = C.Name,
}).ToList());
}
}
return Types;
}
[EDIT]
I've manage to do a sql query to do the job:
select T.TypeID from Type T join SubType ST on St.TypeID=T.TypeID
where ST.SubTypeID=3
But I don't know how to transform that to a linq query and do a Type.AddRange().
Can someone help me with that?
You can use the Intersect method to find the types that include any subtypes from the list of provided subtypes. This also eliminates the need to iterate using foreach and leaving that for Linq to handle.
List<int> subTypes = ListSubTypes.Select(s => int.Parse(s)).ToList();
DataBase.Type.Where(s => s.SubType.Select(st => st.SubTypesID).Intersect(subTypes).Any())
Here's an example based off of your code.
public static List<DropdownModel> GetTypesBySubTypes(List<string> ListSubTypes)
{
List<DropdownModel> Types = new List<DropdownModel>();
List<int> subTypes = ListSubTypes.Select(s => int.Parse(s)).ToList();
using (DocumentXtractorEntities DataBase = new DocumentXtractorEntities())
{
Types.AddRange((from C in DataBase.Type
.Where(s => s.Active
&& subTypes.Intersect(s.SubType.Select(st => st.SubTypesID)).Any())
select new DropdownModel()
{
ID = C.TypeID,
Description = C.Name,
}).ToList());
}
return Types;
}
HTH
You can write a similar join query to how you wrote your sql.
from C in DataBase.Type
join s in DataBase.SubType.Where(s => s.Active && s.SubTypeId == SubTypeIDINT) on C.TypeID equals s.TypeID
select new DropdownModel()
{
ID = C.TypeID,
Description = C.Name,
}

Getting values from Session variable

I have this piece of code
var myList = (from p in db.Full
where ((p.date_reception > begin & p.date_reception < end & !p.mc_host_class.Contains("NULL")) &
(!strListe.Contains(p.mc_host_class)))
group p by p.mc_host_class into g
orderby g.Count() descending
select new
{
hostclassx = g.Key,
countx = g.Count()
}).Take(10).ToList();
HttpContext.Current.Session["allList"] = myList;
i want to get two type of values from my session variable , before using session variable i used to do
object[] ys = myList.Select(a => (object)a.countx.ToString()).ToArray();
List<String> xs = new List<string>();
foreach (var x in myList.Select(i => i.hostclassx))
{
xs.Add(x);
}
I want to get the same type of variables(xs and ys) from my session variable
You have stored an anonymous object inside the session. Anonymous objects are not intended to be leaving the boundaries of the current method. So start by defining a model:
public class MyModel
{
public string HostClass { get; set; }
public int Count { get; set; }
}
and then project your query into this object (bear in mind that you might need to adjust the type of the HostClass property according to your needs - I have defined it as a string but in your particular model it might be some other type, it's just not clear what types of objects are involved in your queries from the code you have pasted so far):
...
orderby g.Count() descending
select new MyModel
{
HostClass = g.Key,
Count = g.Count()
}).Take(10).ToList();
Alright, now you've got a List<MyObject> stored inside your Session["allList"]. So in order to retrieve this value somewhere else in your code it's just a matter of casting back to this same type:
var list = (List<MyModel>)HttpContext.Current.Session["allList"];
And as a side note you seem to be using a & operator instead of && in your where predicates, maybe you didn't exactly wanted to use this. You seem to be confusing the logical AND operator and the binary AND operator.

Join table with object list

I have a table, lets say tblCar with all the related columns like Id, Make, Model, Color etc.
I have a search model for car containing two params Id and Model.
public class CarSearch
{
public int Id { get; set; }
public string Model { get; set; }
}
var carSearchObjets = new List<CarSearch>();
With list of primitive data (like Id list), to get cars with those Ids I could have done:
var idList = new List<int> { 1, 2, 3 };
var carsFromQuery = context.Cars.Where(x => idList.Contains(x.Id);
But if I have to fetch all the cars with Id and model from the list, how do I do it? Simple join cannot be done between in memory objects and tables.
I need something like,
from m in context.Cars
join n in carSearchObjets
on new { Id = n.Id, Model = n.Model } equals new { Id = m.Id, Model = m.Model }
select m;
This obviously won't work.
Please ignore any typos.And if you need more info or the question is not clear, let me know.
One (ugly-but-working) way to manage that is to use concatenation with a "never used" concat char.
I mean a char that should never appear in the datas. This is always dangerous, as... never is never sure, but you've got the idea.
For example, we'll say that our "never used" concat char will be ~
This is not good for perf, but at least working :
var carSearchObjectsConcatenated = carSearchObjets.Select(m => new { m.Id + "~" + m.Model});
then you can use Contains again (concatenating on the db too) : you'll need to use SqlFunctions.StringConvert if you wanna concatenate string and numbers on the db side.
var result = context.Cars.Where(m =>
carSearchObjectsConcatenated.Contains(SqlFunctions.StringConvert((double)m.Id) + "~" + m.Model);
EDIT
Another solution would be to use PredicateBuilder, as mentionned by Sorax, or to build your own Filter method if you don't want a third party lib (but PredicateBuilder is really fine).
Something like that in a static class :
public static IQueryable<Car> FilterCars(this IQueryable<Car> cars, IEnumerable<SearchCar> searchCars)
{
var parameter = Expression.Parameter(typeof (Car), "m");
var idExpression = Expression.Property(parameter, "Id");
var modelExpression = Expression.Property(parameter, "Model");
Expression body = null;
foreach (var search in searchCars)
{
var idConstant = Expression.Constant(search.Id);
var modelConstant = Expression.Constant(search.Model);
Expression innerExpression = Expression.AndAlso(Expression.Equal(idExpression, idConstant), Expression.Equal(modelExpression, modelConstant));
body = body == null
? innerExpression
: Expression.OrElse(body, innerExpression);
}
var lambda = Expression.Lambda<Func<Car, bool>>(body, new[] {parameter});
return cars.Where(lambda);
}
usage
var result = context.Cars.FilterCars(carSearchObjets);
this will generate an sql looking like
select ...
from Car
where
(Id = 1 And Model = "ax") or
(Id = 2 And Model = "az") or
(Id = 3 And Model = "ft")
'PredicateBuilder' might be helpful.
var predicate = PredicateBuilder.False<Car>();
carSearchObjects
.ForEach(a => predicate = predicate.Or(p => p.Id == a.Id && p.Model == a.Model));
var carsFromQuery = context.Cars.AsExpandable().Where(predicate);
Note the text in the link regarding EF:
If you're using Entity Framework, you'll need the complete LINQKit -
for the AsExpandable functionality. You can either reference
LINQKit.dll or copy LINQKit's source code into your application.
Old school solution..
//in case you have a
List<CarSearch> search_list; //already filled
List<Cars> cars_found = new List<Cars>();
foreach(CarSearch carSearch in search_list)
{
List<Cars> carsFromQuery = context.Cars.Where(x => x.Id == carSearch.Id && x.Model == carSearch.Model).ToList();
cars_found.AddRange(carsFromQuery);
}
Abd don't worry about the for loops.
I landed up passing in an xml list as a parameter to the sql query and joined to that:
var xml = new XElement("Cars", yourlist.Select(i => new XElement("Car", new XElement("Id", i.Id), new XElement("Model", i.Model))));
var results = Cars
.FromSql("SELECT cars.*"
+ "FROM #xml.nodes('/Cars/Car') Nodes(Node)"
+ "JOIN Cars cars on cars.Id = Nodes.Node.value('Id[1]', 'int') and cars.Model = Nodes.Node.value('Model[1]', 'varchar(100)')",
new SqlParameter("#xml", new SqlXml(xml.CreateReader())));
For entity-framework-core users I created a nuget package extension:
EntityFrameworkCore.SqlServer.Extensions.Contains

How to group rows using LINQ to Entity separating values with commas?

Below is my LINQ Query, that im using to select ITEMS:
protected void Page_Load(object sender, EventArgs e)
{
whatsmydiscountEntities ctx = new whatsmydiscountEntities();
int IdRelationshipItems = Convert.ToInt32(Request.QueryString["IdRelationshipItems"]);
int IdProductService = Convert.ToInt32(Request.QueryString["IdProductService"]);
var Items = (from es in ctx.State
join ie in ctx.ItemsStates on es.StateId equals ie.StateId
join i in ctx.Items on ie.IdItem equals i.IdItem
join iir in ctx.ItemsRelationshipItems on i.IdItem equals iir.IdItem
join ir in ctx.RelationshipItems on iir.IdRelationshipItems equals ir.IdRelationshipItems
join ips in ctx.ItemsProductsServices on i.IdItem equals ips.IdItem
join ps in ctx.ProductsServices on ips.IdProductService equals ps.IdProductService
where iir.IdRelationshipItems == IdRelationshipItems
&& ips.IdProductService == IdProductService
&& ir.Active == 1
&& i.Active == 1
select new
{
ItemName = i.Name,
StateSigla = es.Sigla,
ProductServiceName = ps.Ttitle,
RelationshipItemName = ir.Name,
RelationshipItemImage = ir.Image,
RelationshipItemActive = ir.Active,
ItemSite = i.Site,
ItemDescription = i.Description,
ItemAddress = i.Address,
Iteminformationdiscount = i.information_discount,
ItemLogo = i.Logo,
ItemActive = i.Active,
StateId = ie.StateId,
IdRelationshipItems = iir.IdRelationshipItems,
IdProductService = ips.IdProductService
}).ToList();
}
As you can see, the result will be 1 row for each state, if the user passes the IdRelationshipItems and the IdProductService.
Instead of 1 row for each state with the same information, I'd like to show only 1 row and all the states separated by commas. What do I need to change to do this?
I had to solve this problem today. I have a view model that looks like this:
public class IndexViewModel
{
public string Search { get; set; }
public IPagedList<MembershipUser> Users { get; set; }
public IEnumerable<string> Roles { get; set; }
public bool IsRolesEnabled { get; set; }
public IDictionary<string,string> Tags { get; set; }
}
I needed to return a unique list of users and all of the Tags that are assigned to them as a comma separated string.
The data is stored like this:
"41FFEC0F-B920-4839-B0B5-862F8EDE25BD", tag1
"41FFEC0F-B920-4839-B0B5-862F8EDE25BD", tag2
"41FFEC0F-B920-4839-B0B5-862F8EDE25BD", tag3
And I needed output that looked something like this:
"41FFEC0F-B920-4839-B0B5-862F8EDE25BD", "tag1, tag2, tag3"
I ended up creating a List of UserId's like this (I'm using the MembershipProvider which exposes the UserId as ProviderUserKey):
var userIdList = users.Select(usr => (Guid) usr.ProviderUserKey).ToList();
The object "users" is a MembershipUser object. I then call a function in my service passing the List in like this:
Tags = _usersTagsService.FindAllTagsByUser(userIdList)
And my service function looks like this:
public IDictionary<string, string> FindAllTagsByUser(IList<Guid> users)
{
var query = (from ut in _db.UsersTags
join tagList in _db.Tags on ut.TagId equals tagList.Id
where users.Contains(ut.UserId)
select new {ut.UserId, tagList.Label}).ToList();
var result = (from q in query
group q by q.UserId
into g
select new {g.Key, Tags = string.Join(", ", g.Select(tg => tg.Label))});
return result.ToDictionary(x=>x.Key.ToString(),y=>y.Tags);
}
I'm pretty sure these two linq statements can probably be combined into one but I find it easier to read this way. Still only hits the database once.
Things to watch out for:
I was originally passing just IList users into the function FindAllTagsByUser and linq couldn't infer the type and therefore wouldn't let me use the .Contains extension method. Kept saying that linq to entities didn't support Contains.
You need to do a .ToList() on the first query to materialize it or you will get trouble from linq to entity when you try to use string.Join to create the comma separated list.
Good luck
dnash

Modifying Linq Results

Very basic question. How do I do modify Linq results?
To expound further, I selected an order list from an order table in the database. I need to display the results in a gridview on a web form. First, I need to modify some of the results. For instance, the "Quote" field should be changed to blank when the "Order" field has a value. It is likely I might have to do more elaborate manipulation of the results. In the past, it was possible to loop through arrays, and modify them, but today's programming seems to not want loops happen. At the moment the results seem to be read-only, as if I am doing something wrong by needing to modify the list.
protected void fillGridView()
{
using (CqsDataDataContext cqsDC = new CqsDataDataContext())
{
var orderlist = from x in cqsDC.MasterQuoteRecs
where x.CustomerNumber == accountNumber && x.DateCreated > DateTime.Now.AddDays(howfar)
orderby x.DateCreated descending
select new
{
customer = x.customername,
order = x.OrderReserveNumber,
quote = x.Quote,
date = Convert.ToDateTime(x.DateCreated).ToShortDateString(),
project = x.ProjectName,
total = x.Cost,
status = x.QuoteStatus
};
// I would like to loop thru list and make changes to it here
GridView1.DataSource = orderlist;
GridView1.DataBind();
}
}
You end up with an IQueryable<anonymoustype> with your current query. Since they're anonymous types they're readonly and can't be changed anyway.
Your best option, especially if you intend to have more complex manipulations that can't be done in the query by the database, is to use a class instead. You'll also want to add a ToList() at the end of your query so you end up with a List<YourClass> and can then loop over it as you usually would and change the objects.
So make a class that has all your properties, for example MasterQuote, and use that in your query instead:
var query = from x in cqsDC.MasterQuoteRecs
where x.CustomerNumber == accountNumber && x.DateCreated > DateTime.Now.AddDays(howfar)
orderby x.DateCreated descending
select new MasterQuote
{
Customer = x.customername,
Order = x.OrderReserveNumber,
Quote = x.Quote,
Date = Convert.ToDateTime(x.DateCreated).ToShortDateString(),
Project = x.ProjectName,
Total = x.Cost,
Status = x.QuoteStatus
};
var orderList = query.ToList();
foreach (var item in orderList)
{
if (item.OrderReserveNumber > 0)
{
item.Quote = "";
}
}
Your MasterQuote class would look something like:
public class MasterQuote
{
public string Customer { get; set; }
public int Order { get; set; }
// and so on
}
Of course for your given example you could probably accomplish the Quote manipulation in your query as seth mentioned.
Just use a ternary operator.
select new
{
customer = x.customername,
order = x.OrderReserveNumber,
quote = x.OrderReserveNumber != null ? string.Empty : x.Quote,
date = Convert.ToDateTime(x.DateCreated).ToShortDateString(),
project = x.ProjectName,
total = x.Cost,
status = x.QuoteStatus
};
Depends on how complex the changes are really, but the one you mentioned could be done with a simple method on a static class.
Of you could just chain linq statements together, and do the equivalent of sql case for the quote column.
It seems unlikely that OrderList doesn't implement IEnumerable so if all the linq gets too messy foreach will do the job.

Categories

Resources