how to insert an array of id's into linq-to-sql query line in .net mvc 3 - asp.net-mvc-3

i want to get an array of id's which is like ["15", "26", "37", "48", "90"] and i want to get my remaining items from my remaining table that doesnt includes these supplier id's..
here what i done so far:
string[] arrgroupdetails;
arrgroupdetails = dataContext.GroupDetails.Select(c => c.supplier_id).ToArray();
var items = from thingies in dataContext.remainings where thingies.supplier_id.ToString() != arrgroupdetails.Any().ToString() select thingies;
so how can i achive this?

By heart, so do check syntax but someething like this should work:
var items = from thingies in dataContext.remainings
where !arrgroupdetails.Contains(thingies.supplier_id.ToString())
select thingies;

Related

Index duplicates from the list using Linq

Suppose I have a list of strings
var data = new List<string>{"fname", "phone", "lname", "home", "home", "company", "phone", "phone"};
I would like to list all values and add index to duplicates like this
fname,
phone,
lname,
home,
home[1],
company,
phone[1],
phone[2]
or like this
fname,
phone[0],
lname,
home[0],
home[1],
company,
phone[1],
phone[2]
The both solutions would work for me.
Is that possible with Linq?
You can use LINQ GroupBy to gather the matches, and then the counting version of Select to append the indexes.
var ans = data.GroupBy(d => d).SelectMany(dg => dg.Select((d, n) => n == 0 ? d : $"{d}[{n}]"));

LINQ sorting by number of appearances

I need to sort a list by numer od appearances and show repeating items only once. For example I have a following list : "a", "b", "a", "a", "c", "d", "c". I want it to be sorted like this: "a","c","b",d". How can I achieve this?
That is what I made:
var something = from c in db.Letters
group c by c.letter into p
orderby p.Count()
select new
{
p.letter
};
But expression p.letter cannot be used.
var resunt = from c in db.Letters
group c by c.letter into p
orderby p.Count() descending
select p.Key;
When you use GroupBy you get an IGroupping object that contains a property of the Key object that you've grouped by and a collection of the values in the group. In your case you want to return the Key - the c.letter

Modifying an array in Swift 2

shoppingList[4...6] = ["Bananas", "Oranges"]
Don't use subscript syntax to append a new item to an array.
But I realize append one or more new items to array with ranges successed. For example:
var shoppingList : Array<String> = ["Bread", "Milk", "Blackingbar", "Chian", "baba", "Oppo", "Xiaomi"]
shoppingList[4...6] = ["Bananas", "Oranges", "Huewei", "5", "6"]
for obj in shoppingList {
print(obj)
}
Why we have recommeded "don't use it" ????
The precise quote from the documentation is
You can’t use subscript syntax to append a new item to the end of an
array.
Your code example replaces three items at index 4 - 6 with five other items simultaneously which is something different. As far as the subscripted range is not out of bounds the operation succeeds.
Im not sure why you would want to make 5 items replace the last 3 but if you need to do it, you could do this:
var shoppingList : Array<String> = ["Bread", "Milk", "Blackingbar", "Chian", "baba", "Oppo", "Xiaomi"]
shoppingList = shoppingList[0..<4] + ["Bananas", "Oranges", "Huewei", "5", "6"]

Use of distinct() in dropdownlistFor

I am trying to use distinct() in my dropdownlistFor in order to get rid of duplicates.
Model.Categories contains a list of strings.
Been trying different "combinations" but cant get it to work..I thought the below example would do the tric but it still shows duplicates.
#Html.DropDownListFor(model => model.CatDropTemp,
Model.Categories.Select(kat => new SelectListItem { Text = kat, Value = kat }).Distinct())
Do I maybe have to change the whole construction of the list or is there something im missing?
EDIT:
Found this syntax here at the forum:
#Html.DropDownList(
"Foo",
new SelectList(
Model.Categories.Select(x => new { Value = x, Text = x }).Distinct(),
"Value",
"Text"
)
)
That takes care of the Distinct()-part, however...With this syntax I cant see where I add my
new{#class="form-control"}
Any tips on that maybe?
I think you need to do your distinct earlier in the chain, it doesn't look like SelectListItem implements any Equality/Comparison interfaces (assuming your model overrides Equals and GetHashCode)

Using Linq to create crosstab results [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is it possible to Pivot data using LINQ?
I'm wondering if its at all possible to create crosstab style results with Linq.
I have some data that looks like the following:
var list = new[]
{
new {GroupId = 1, Country = "UK", Value = 10},
new {GroupId = 1, Country = "FR", Value = 12},
new {GroupId = 1, Country = "US", Value = 18},
new {GroupId = 2, Country = "UK", Value = 54},
new {GroupId = 2, Country = "FR", Value = 55},
new {GroupId = 2, Country = "UK", Value = 56}
};
and I'm trying to output to a repeater control something like the following:
GroupId.....UK.....FR.....US
1...........10.....12.....18
2...........54.....55.....56
Its the dynamic columns that are causing my problems. Any solutions to this?
You need a runtimy class to hold these runtimy results. How about xml?
XElement result = new XElement("result",
list.GroupBy(i => i.GroupId)
.Select(g =>
new XElement("Group", new XAttribute("GroupID", g.Key),
g.Select(i => new XAttribute(i.Country, i.Value))
)
)
);
Are you expecting multiple records per result cell? If so there would need to be some Summing (and more grouping) in there.
(this answer is proof of concept, not final result. There's several issues to address, such as: ordering of columns, missing cells, and so on).
After doing a quick search you might want to look at the ModuleBuilder, TypeBuilder, and FieldBuilder classes in System.Reflection.Emit. They allow you to create a class dynamically at runtime. Outside of that you would need to do grouping on your objects and then do something with the hierarchical results you get from LINQ. I am not sure of a way to dynamically create anonymous type fields at runtime, and that sounds like what would need to happen.
You could try using the dynamic linq library provided by MS. They have a number of overloads to extensions methods that take strings as arguments. They also have an expression parser that takes a string an emits a lambda expression. You should be able to create a dynamic select using them.
A word of warning though, you end up with a non-generic IQueryable rather than a generic IQueryable so you are a little bit limited on what you can do with the result, and you give up a bit of type safety, but that may be OK in your application...
The link for the dynamic linq stuff is
http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
There is a link where you can download the source code the the dynamic library, plus some nice illustrations of how you can use it.
var labResults = from lab in CoreLabResults
where lab.Patient == 8
group lab by new { lab.Patient, lab.TestNo, lab.CollectedDate }
into labtests
select new
{
labtests.Key.Patient,
labtests.Key.TestNo,
labtests.Key.CollectedDate,
MCHC = labtests.Where(lab => lab.TestVar == "MCHC").FirstOrDefault().Result,
LYABS = labtests.Where(lab => lab.TestVar == "LYABS").FirstOrDefault().Result,
TotalTests = labtests.Count()
}

Resources